做好了开关换挡的代码判断P32的电平来决定换挡还是不换.
现在有时灵有时又不灵. 电路图,代码如下
求大神帮下我.
- #include "stc8g.h"
- #include "intrins.h"
- #include "eeprom.h"
- #include <stdio.h>
-
- #define MAIN_Fosc 11059200UL
- #define BRT (65536 - MAIN_Fosc / 115200 / 4)
- //#define EEPROMId 0x0E00
-
- typedef unsigned char u8;
- typedef unsigned int u16;
- //u8 PWMData[] = {181,111,51,2};//添加数组,设置需要的档位
- u8 PWMData[] = {2,8,31};//添加数组,设置需要的档位
- u16 delayTime = 3000;//这里修改需要的工作时间,单位(ms)
-
- u8 dat;
- u16 EEPROMId = 0020;
- u16 time;
- sbit CAP = P3^2;
- u8 iscap=0;
- u8 mode = 0;
- void delayms(u16 ms);
- void sys_init();
- void PWM_init(void);
- void Timer0_Init(void);
-
- void UartInit()
- {
- SCON = 0x5a;
- TL1 = BRT;
- TH1 = BRT >> 8;
- TR1=1;
- AUXR = 0x40;
- }
-
- u8 LEDControl()
- {
-
-
- u8 i;
- u8 length = (sizeof(PWMData)/sizeof(PWMData[0]));
- delayms(20);
- // u16 EEPROMId = 0XE00; //将指针指向第8扇区的第一个字节
- // SetMode(CMD_READ); //设置为读取数据
-
-
-
- while(1)
- {
-
-
- dat = IapRead(EEPROMId + 1); //读取下一字节数据255
-
-
-
- if(dat == 0XFF) //下一字节字节未写入数据255
- {
-
- dat = IapRead(EEPROMId); //读取当前字节数据
- // SetMode(CMD_PROGRAM); //写入数据
-
-
-
- for(i = 0; i < length-1; i++)
- { if(dat == i)
- {
- if(iscap)
- {
- IapProgram(EEPROMId+1,i+1);
- }
- return dat;
- }
- }
-
- // dat=0;
- IapProgram(EEPROMId+1,0);
-
- return dat; //返回当前字节数据
- }
- else //下一字节不为空,指针加一,继续判断下一个字节
- { EEPROMId++;
- }
- if(EEPROMId >= 0XFFD) //如果当前字节到达EEPROM末尾,擦除扇区所有数据
- {
- // SetMode(CMD_ERASE); //扇区擦除
- IapErase(0x0000); //擦除扇区
- IapErase(0x0200); //擦除扇区
- IapErase(0x0400); //擦除扇区
- IapErase(0x0600); //擦除扇区
- IapErase(0x0800); //擦除扇区
- IapErase(0x0A00); //擦除扇区
- IapErase(0x0C00); //擦除扇区
- IapErase(0x0E00); //擦除扇区
- return 1;
- }
- }
- }
- void main()
- {
-
- sys_init();
- iscap=CAP;
- P3M0 = 0x04; P3M1 = 0x00; //P32推挽输出对电容充电
- LEDControl();
- UartInit();
-
-
- PWM_init();
-
-
- CCAP1H = PWMData[dat] ;
-
- while(1)
- {
- printf("id=%u\n",EEPROMId);
- printf("cap=%bu\n",CCAP1H);
- printf("cap=%bu\n",iscap);
- delayms(1000);
-
- }
- }
- void sys_init(void)
- {
- P_SW2=0x80;
- // P3M0 = 0x04; P3M1 = 0x00;
-
- Timer0_Init();
-
- IT0=1;EX0=1;
- INTCLKO|=0x40; //EX4=1; 允许INT4外部中断
- // IP2H=0x00;
- IP2=0x10;
-
-
- // ES = 1; //关闭串口中断,可以双击换挡
- EA=1; //打开总中断
- }
-
- //// 初始化PWM功能
- void PWM_init(void)
- {
- CCON = 0x00;
- CMOD = 0x08;
- CL = 0x00;
- CH = 0x00;
- CCAPM1 = 0x42;
- PCA_PWM1 = 0x00;
- CCAP1L = 0x00;
- CCAP1H = 0x00;
- CR=1;
- }
-
-
- //u8 TimeNum = 1;//这里是定时器中断时间,单位(ms)
- void Timer0_Isr(void) interrupt 1
- {
- // u8 Num = delayTime/TimeNum;//中断执行多少次到达工作时间
- // if(time < Num)
- // { time++;
- // }else if(time == Num) //到达要求的时间
- // { IapProgram(EEPROMId+2,dat);
- // time++;
- // }
-
-
-
- }
- void Timer0_Init(void) //1毫秒@11.0592MHz
- {
- AUXR |= 0x80; //定时器时钟1T模式
-
-
- TMOD &= 0xF0; //设置定时器模式
- TL0 = 0xCD; //设置定时初始值
- TH0 = 0xD4; //设置定时初始值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- ET0 = 1; //使能定时器0中断
- }
- void INT4_Isr(void) interrupt 16
- {
- IAP_CONTR=0x60;
- }
- void INT0_Isr(void) interrupt 0
- {
- // printf("dat=%bu\r\n",dat);
- // printf("id=%u\r\n",EEPROMId);
- }
- void delayms(u16 ms)
- {u16 i;
- do{
- i = MAIN_Fosc /10000;
- while(--i);
- } while(--ms);
- }
-
复制代码
|