1、unsigned char D; /占空比10倍unsigned HT; /高电平时间(us)unsigned int N0_MIN=150;unsigned int N0_MAX=2048;unsigned int N0; /期望转速值unsigned int N; /实际测速值unsigned char DN; /转速误差unsigned long n;char WW,QW,BW,SW,GW;unsigned char T0N;unsigned int DN_OLD;unsigned int T=0;unsigned int N1;unsigned int N2;int AD_SUB(vo
2、id);void CSH(void);void DISP_N0_DN(unsigned int,unsigned char);void DISP_N_D(unsigned int,unsigned char);void CAP_CSH(void);void PWM_CSH(unsigned int);void DUTY_SET(unsigned int);void AD_CSH(void);void LCD_CSH(void);void LCD_WRITE_4(char,char);void LCD_WRITE(char,char);void LCD_BUSY(void);char LCD_R
3、EAD(void);void DISP_C(char);void DISP_MENU(const char *A);void BCD(unsigned int);void DELAY(unsigned int);void DELAY_I(unsigned int);void DELAY_US(char);void interrupt INT_ISR(void);void change(void);/unsigned int DN_GET(void);/宏定义:清屏#define CLR_LCD() LCD_WRITE(0b00000001,COM); DELAY(2)/AD转换,期望转速N0取
4、值N0_MINN0_MAX,并放大100倍 void main(void) unsigned int i; CSH(); DISP_MENU(*MENU0); DELAY(100); / DELAY(400); CLR_LCD(); while(1) N0=AD_SUB(); DISP_N0_DN(N0,DN); /显示期望转速和转速误差允许值 DISP_N_D(N,D); change(); /显示实际转速和占空比 while (KEY.FLAG=0) DELAY(1); i+; if (i200) LED=0;400) break; DN_OLD=DN;while(KEY.FLAG=1)
5、if (KEY.SET=1) /进入修改,字符闪 LCD_WRITE(DN_POSITION,COM); /确定字符闪的位置 LCD_WRITE(0b00001111,COM); /字符闪 if (KEY.SETINT=1) if (KEY.SET=1) N1=NDT; DELAY_US(3); KEY.SETINT=0;T+; if(T10)T=0; KEY.CHANGE=1; if (KEY.INC=1) if (KEY.SET=1)DN+; KEY.INC=0; KEY.CHANGE=1; if (KEY.DEC=1) if (KEY.SET=1)DN-; KEY.DEC=0; if (
6、KEY.CHANGE=1) /如果有修改,刷新显示 DISP_N0_DN(N0,DN); KEY.CHANGE=0; if (KEY.SET=1) /刷新后重新开字符标闪 LCD_WRITE(DN_POSITION,COM); LCD_WRITE(0b00001101,COM); if (DN_OLD!=DN) EEPROM_WRITE(1,DN); /写入速度控制的回差值,在EEPROM的地址1 void change(void) unsigned int j; if(NN0+DN) HT-; if(HT1000)HT=1000; DUTY_SET(HT); DELAY(300); else
7、 if(NN0-DN) HT+;1000)HT=0; LED=1; j=0;void CSH(void) ANSELH=0;/关闭B口模拟引脚,设为I/O引脚 TRISB=0b00001111; OPTION=0b00010111;/B口上拉,且下降沿中断 INTCON=0b11010000;/GIE=1;PEIE=1;INTE=1;RBIE=1; IOCB=0b00001110;/B口电平变化中断控制寄存器,RB1、RB2电平变化产生中断,在RBIE=1的前提下 KEY.FLAG=0; KEY.SET=0; TMR1H=TMR1L=0; T1CON =0b00110001;/1:8分频 TR
8、ISD=0b00000000; TRISC=0b00100000; HT=EEPROM_READ(0); DN=EEPROM_READ(1); LCD_CSH(); CAP_CSH(); PWM_CSH(HT); AD_CSH();int AD_SUB(void) GODONE=1; while(GODONE=1); ADIF=0; AD_RESULT.AD0=ADRESL; AD_RESULT.AD1=ADRESH; n=AD_RESULT.AD_TEMP; n*=350; N2=n/1023+N1; return N2;void DISP_N0_DN(unsigned int START_
9、N0,unsigned char DELTA_N) LCD_WRITE(0b00001100,COM); /不闪 LCD_WRITE(LINE2,COM); /即第2行的第0个字符 BCD(START_N0); /将要显示的起动占空比值做BCD转换 LCD_WRITE(N,DAT); /在当前位置显示字符0=: LCD_WRITE(QW+0x30,DAT); /显示千位 LCD_WRITE(BW+0x30,DAT); /显示百位 LCD_WRITE(SW+0x30,DAT); /显示十位 LCD_WRITE(GW+0x30,DAT); /显示个位rp/mm /在当前位置显示空格 BCD(DEL
10、TA_N); /将要显示的转速n控制回差量做BCD转换Dnvoid DISP_N_D(unsigned int R1,unsigned char DUTY) /速度在R1,占空比DUTY LCD_WRITE(0b00001100,COM); BCD(R1); LCD_WRITE(LINE1,COM); /DDRAM地址,第1行的第5个字符 BCD(DUTY); /将要显示的转速n控制回差量做BCD转换% /显示字符void CAP_CSH(void) TRISC2=1; CCP1CON=0b00000110; T1CON=0b00110001; CCP1IF=0; CCP1IE=1; GIE=
11、1; PEIE=1;void interrupt INT_ISR(void) long X; unsigned int TZ; if(CCP1IF=1) TMR1H=TMR1L=0; TZ=(CCPR1H1) KEY.SET=0; KEY.FLAG=0; RBIE=0; / TMR1ON=1; RBIE=1; / PEIE=1; / GIE=1; T0IE=1; TMR0=0; T0IF=0; T0N=0; if (RBIF=1) if (RB3=0 & KEY.FLAG=1 ) KEY.SETINT=1; T0N=0; if (RB2=0 & KEY.FLAG=1) KEY.DEC=1; i
12、f (RB1=0 & KEY.INC=1; RBIF=0; if(T0IF=1) T0IF=0; T0N+; if (T0N100) /约6S KEY.FLAG=0; KEY.SET=0; T0IE=0;/设置周期为1000US的PWM输出 void PWM_CSH(unsigned int ht) PR2=249; DUTY_SET(ht); /X=0b0011001000; T2CON=0b00000101; /预分频比为1:4 /设置PWM波的高电平时间void DUTY_SET(unsigned int ht) CCPR2L=(ht2) & 0x0FF; CCP2CON=(ht4) &
13、 0x0F; /先写入高4位 LCD_WRITE_4(R2,FLAG); R2=R1 & /后写入低4位 /延时100us void LCD_BUSY(void) unsigned char R1; R1=LCD_READ(); /读寄存器 if(R1&0x80)=0x00) /最高位为忙标志位 break;char LCD_READ(void) TRISD |=0xF0; /D口低4位为输入 /选择命令 LCD_RW=1; /写命令模式 NOP(); /LCD使能 R1=0; R1=PORTD & R1=(R1 0xF0; /读数据的高4位给R1高4位 /使能端形成下降沿 R1 |=PORT
14、D & /读数据的低4位给R1低4位 TRISD &=0x0F; /D口低4位恢复为向LCD输出 return R1;void DISP_C(char line) char i; LCD_WRITE(line,COM); for(i=0;i16;i+) LCD_WRITE(DDi,DAT);void DISP_MENU(const char *A) char i,j;2; for(j=0;jj+) DDj=*A+; A+; DISP_C(LINEi);/从R1双字节数转换为十进制数万位-个位:WW,QW,BW,SW,GWvoid BCD(unsigned int R1) /延时n msvoid DELAY(unsigned int n)void DELAY_I(unsigned int n)/延时(n*10)usvoid DELAY_US(char n)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1