1. ST语言编程参考及示例

1.8. EL2262作为可调频率信号发生器的实现方法

本例使EL2262成为一个频率信号发生器,当nPluse值为2时,且task为1ms,最高频率为500kHz。本例task为10ms,当nPluse值为5000时,频率为20Hz。详见截图

IF NOT initial THEN
nPluse_H:=nPluse/2;
nPluse_L:=nPluse- nPluse_H;
initial:=TRUE;
END_IF
FOR nIdx := 1 TO CnEL2262ovs BY 1 DO
CASE index OF
0:
IF nPluse_H>0 THEN
  abEL2262_Ch1_Output[nIdx] :=TRUE;
  abEL2262_Ch2_Output[nIdx] :=TRUE;
  nPluse_H:=nPluse_H-1;
  IF nPluse_H=0 THEN
   nPluse_H:=nPluse/2;
   index:=10;
  END_IF
END_IF
10: IF nPluse_L>0THEN
  abEL2262_Ch1_Output[nIdx] :=FALSE;
  abEL2262_Ch2_Output[nIdx] :=FALSE;
  nPluse_L:=nPluse_L-1;
  IF nPluse_L=0 THEN
   nPluse_L:=nPluse- nPluse_H;
   index:=0;
  END_IF
END_IF
END_CASE
END_FOR


(*+++++++++++++ 把bit 复制到byte ++++++++++++++++++++++++++*)
FOR nIdx := 1 TO CnEL2262ovs BY 1 DO
nBitPos := (nIdx - 1) MOD 8;
nIdxByte := ((nIdx-1) / 8) + 1; (*number of Byte*)
CASE nBitPos OF
0: abEL2262_ch1_Output_Byte[nIdxByte].0 := abEL2262_ch1_Output[nIdx];
  abEL2262_ch2_Output_Byte[nIdxByte].0 := abEL2262_ch2_Output[nIdx];
1: abEL2262_ch1_Output_Byte[nIdxByte].1 := abEL2262_ch1_Output[nIdx];
  abEL2262_ch2_Output_Byte[nIdxByte].1 := abEL2262_ch2_Output[nIdx];
2: abEL2262_ch1_Output_Byte[nIdxByte].2 := abEL2262_ch1_Output[nIdx];
  abEL2262_ch2_Output_Byte[nIdxByte].2 := abEL2262_ch2_Output[nIdx];
3: abEL2262_ch1_Output_Byte[nIdxByte].3 := abEL2262_ch1_Output[nIdx];
  abEL2262_ch2_Output_Byte[nIdxByte].3 := abEL2262_ch2_Output[nIdx];
4: abEL2262_ch1_Output_Byte[nIdxByte].4 := abEL2262_ch1_Output[nIdx];
  abEL2262_ch2_Output_Byte[nIdxByte].4 := abEL2262_ch2_Output[nIdx];
5: abEL2262_ch1_Output_Byte[nIdxByte].5 := abEL2262_ch1_Output[nIdx];
  abEL2262_ch2_Output_Byte[nIdxByte].5 := abEL2262_ch2_Output[nIdx];
6: abEL2262_ch1_Output_Byte[nIdxByte].6 := abEL2262_ch1_Output[nIdx];
  abEL2262_ch2_Output_Byte[nIdxByte].6 := abEL2262_ch2_Output[nIdx];
7: abEL2262_ch1_Output_Byte[nIdxByte].7 := abEL2262_ch1_Output[nIdx];
  abEL2262_ch2_Output_Byte[nIdxByte].7 := abEL2262_ch2_Output[nIdx];
END_CASE
END_FOR
wEL2262CycCnt_ch1 := wEL2262CycCnt_ch1 + 1;
wEL2262CycCnt_ch2 := wEL2262CycCnt_ch2 + 1;




2018.6.1 余子辰 编辑