|
使用的是STC12C5A60S2芯片,不能仿真实在是很蛋疼啊
dac.c文件:
- #include "stc12c5a60s2.h"
- #include "intrins.h"
- #include "adc.h"
- #include "key.h"
- #include "pwm.h"
- #include "lcd1602.h"
- #define U8 unsigned char
- #define uint unsigned int
- #define uchar unsigned char
- #define ADC_POWER 0x80 //ADC power control bit 电源控制位
- #define ADC_FLAG 0x10 //ADC complete flag 标志位
- #define ADC_START 0x08 //ADC start control bit 启动控制位
- #define ADC_SPEEDLL 0x00 //420 clocks
- #define ADC_SPEEDL 0x20 //280 clocks
- #define ADC_SPEEDH 0x40 //140 clocks
- #define ADC_SPEEDHH 0x60 //70 clocks
- unsigned char in_str[]="0.00V";
- unsigned char d[5];
- uint ch=0, number=0;
- uint light_input, key_input, shi_input;
- void ADC_Init(); // 初始化AD
- void Delay(unsigned int z); // 延时
- void ADC_lcd1602();
- void ADC_start()
- {
- uint a = 0;
-
- PWM_Initial();
- PWM_clock(2); // PCA/PWM时钟源为 定时器0的溢出
- while(1)
- {
- ADC_lcd1602();
- a = KeyPro();
- if(a == 0)
- {
- PWM_start(0,1);
- CCAP0L = 0XC0;
- CCAP0H = 0XC0; //模块0初始输出 占空因数为25%
- PWM_start(1,1);
- CCAP1L = 0XC0;
- CCAP1H = 0XC0; //模块1初始输出 占空因数为25%
- }
- else if(a == 1)
- {
- PWM_start(0,1);
- CCAP0L = 0X80;
- CCAP0H = 0X80; //模块0初始输出 占空因数为50%
-
- PWM_start(1,1);
- CCAP1L = 0X40;
- CCAP1H = 0X40; //模块1初始输出 占空因数为75%
- }
- else if(a == 2)
- {
- PWM_start(0,1);
- CCAP0L = 0X40;
- CCAP0H = 0X40; //模块0初始输出 占空因数为75%
-
- PWM_start(1,1);
- CCAP1L = 0X40;
- CCAP1H = 0X40; //模块1初始输出 占空因数为75%
- }
- else if(a == 3)
- {
- PWM_start(0,1);
- CCAP0L = 0X80;
- CCAP0H = 0X80; //模块0初始输出 占空因数为50%
-
- PWM_start(1,1);
- CCAP1L = 0X80;
- CCAP1H = 0X80; //模块1初始输出 占空因数为50%
- }
- }
- }
- void ADC_lcd1602()
- {
- uint L = 0, K = 0;
- if (number==8000)
- {
- shi_input =shi_input&0xff;
- L = (uint)((4700l*shi_input)/255);
- d[1] = L / 1000; //最高位
- L = L % 1000;
- d[2] = L / 100; //次高位
- L = L % 100;
- d[3] = L /10; //...
- d[4] = L % 10; //....
-
- num_lcdDis(0,0x06,d[4],2);
- num_lcdDis(0,0x05,d[3],2);
- num_lcdDis(0,0x04,d[2],2);
- num_lcdDis(0,0x03,d[1],2);
- num_lcdDis(0,0x02,d[0],2);
- // delay5ms();
- light_input = light_input&0xff;
- K = (uint)((4700l*light_input)/255);
- in_str[1] = K / 1000; //最高位
- K = K % 1000;
- in_str[2] = K / 100; //次高位
- K = K % 100;
- in_str[3] = K /10; //...
- in_str[4] = K % 10; //....
- num_lcdDis(1,0x06,in_str[4],2);
- num_lcdDis(1,0x05,in_str[3],2);
- num_lcdDis(1,0x04,in_str[2],2);
- num_lcdDis(1,0x03,in_str[1],2);
- num_lcdDis(1,0x02,0,2);
- // delay5ms();
- number=0;
- }
- }
- void ADC_Init()
- {
- ADC_RES = 0; //清除上次采集的结果
- IE=0xa0;
- P1ASF=0xe0; //设置AD输入通道为 P1.0-1.2
- ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START|ch ; //选择开启的通道
- Delay(2); //上电启动AD转换的延时
- }
- void Delay(unsigned int n)
- {
- unsigned int x;
- while (n--)
- {
- x=5000;
- while (x--);
- }
- }
- void ADC_isr(void) interrupt 5 using 1 //中断服务程序,每100ms读一次AD
- {
- ADC_CONTR &= !ADC_FLAG;
- if (number==20)
- {
- if (ch==0 )
- {
- shi_input= ADC_RES;
- }
- else if(ch==1)
- {
- light_input= ADC_RES;
- //light_input = 127;
- }
- //else if (ch==2) { key_input= ADC_RES; }
- if(++ch>1)
- ch=0;
- }
-
- number++;
- ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START|ch ;
- }
复制代码
pwm.c
|
评分
-
查看全部评分
|