|
使用的是STC12C5A60S2芯片,不能仿真實(shí)在是很蛋疼啊
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 標(biāo)志位
- #define ADC_START 0x08 //ADC start control bit 啟動(dòng)控制位
- #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); // 延時(shí)
- void ADC_lcd1602();
- void ADC_start()
- {
- uint a = 0;
-
- PWM_Initial();
- PWM_clock(2); // PCA/PWM時(shí)鐘源為 定時(shí)器0的溢出
- while(1)
- {
- ADC_lcd1602();
- a = KeyPro();
- if(a == 0)
- {
- PWM_start(0,1);
- CCAP0L = 0XC0;
- CCAP0H = 0XC0; //模塊0初始輸出 占空因數(shù)為25%
- PWM_start(1,1);
- CCAP1L = 0XC0;
- CCAP1H = 0XC0; //模塊1初始輸出 占空因數(shù)為25%
- }
- else if(a == 1)
- {
- PWM_start(0,1);
- CCAP0L = 0X80;
- CCAP0H = 0X80; //模塊0初始輸出 占空因數(shù)為50%
-
- PWM_start(1,1);
- CCAP1L = 0X40;
- CCAP1H = 0X40; //模塊1初始輸出 占空因數(shù)為75%
- }
- else if(a == 2)
- {
- PWM_start(0,1);
- CCAP0L = 0X40;
- CCAP0H = 0X40; //模塊0初始輸出 占空因數(shù)為75%
-
- PWM_start(1,1);
- CCAP1L = 0X40;
- CCAP1H = 0X40; //模塊1初始輸出 占空因數(shù)為75%
- }
- else if(a == 3)
- {
- PWM_start(0,1);
- CCAP0L = 0X80;
- CCAP0H = 0X80; //模塊0初始輸出 占空因數(shù)為50%
-
- PWM_start(1,1);
- CCAP1L = 0X80;
- CCAP1H = 0X80; //模塊1初始輸出 占空因數(shù)為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; //清除上次采集的結(jié)果
- IE=0xa0;
- P1ASF=0xe0; //設(shè)置AD輸入通道為 P1.0-1.2
- ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START|ch ; //選擇開啟的通道
- Delay(2); //上電啟動(dòng)AD轉(zhuǎn)換的延時(shí)
- }
- void Delay(unsigned int n)
- {
- unsigned int x;
- while (n--)
- {
- x=5000;
- while (x--);
- }
- }
- void ADC_isr(void) interrupt 5 using 1 //中斷服務(wù)程序,每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 ;
- }
復(fù)制代碼
pwm.c
|
-
-
ADC和PWM波可調(diào).zip
2016-4-19 12:03 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
74.49 KB, 下載次數(shù): 77, 下載積分: 黑幣 -5
STC12C5A60S2 ADC PWM
評(píng)分
-
查看全部評(píng)分
|