STC12C5A60S2的ADC+PWM功能
STC12C5A60S2單片機(jī)的A/D轉(zhuǎn)換口在P1口(P1.7-P1.0),有8路10位高速A/D轉(zhuǎn)換器,速度可達(dá)到250 KHz(25萬次/秒)。脈寬調(diào)制(PWM)是一種使用程序來控制波形占空比、周期、相位波形的技術(shù)。STC12C5A60S2單片機(jī)的PAC模塊可以通過程序設(shè)定,使其工作于8位PWM模式。
下面是一段將ADC和PWM結(jié)合起來應(yīng)用的程序:
/***************************************************************時間:2012.12.1晶振:12MHz功能描述:AD采集電位器的電壓信號,然后信號以PWM信號輸出控制LED的亮度(調(diào)節(jié)電位器)當(dāng)電位器兩端的電壓大時,LED較亮,同時用1602顯示采集的電壓值 AD采集通道:P1.0PWM輸出: P1.3***************************************************************/#include<12C5A60S2.h>#includeunsigned int result,ge,shifen,baifen;unsigned char seg[10]={0,1,2,3,4,5,6,7,8,9};//要顯示字符sbit RS = P2^4; //命令數(shù)據(jù) sbit RW = P2^5; //寫還是讀sbit EN = P2^6; //使能端#define RS_CLR RS=0 #define RS_SET RS=1#define RW_CLR RW=0 #define RW_SET RW=1 #define EN_CLR EN=0#define EN_SET EN=1#define DataPort P0/*------------------------------------------------uS延時函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值unsigned char 是定義無符號字符變量,其值的范圍是0~255 這里使用晶振12M,精確延時請使用匯編,大致延時長度如下 T=tx2+5 uS ------------------------------------------------*/void DelayUs2x(unsigned char t){ while(--t);}/*------------------------------------------------mS延時函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值unsigned char 是定義無符號字符變量,其值的范圍是0~255 這里使用晶振12M,精確延時請使用匯編------------------------------------------------*/void DelayMs(unsigned char t){while(t--){//大致延時1mSDelayUs2x(245);DelayUs2x(245);}}/*------------------------------------------------判忙函數(shù)------------------------------------------------*/bit LCD_Check_Busy(void) { DataPort= 0xFF; RS_CLR; RW_SET; EN_CLR; _nop_(); EN_SET;return (bit)(DataPort & 0x80);}/*------------------------------------------------寫入命令函數(shù)------------------------------------------------*/void LCD_Write_Com(unsigned char com) { while(LCD_Check_Busy()); //忙則等待RS_CLR; RW_CLR; EN_SET; DataPort= com; _nop_(); EN_CLR;}/*------------------------------------------------寫入數(shù)據(jù)函數(shù)------------------------------------------------*/void LCD_Write_Data(unsigned char Data) { while(LCD_Check_Busy()); //忙則等待RS_SET; RW_CLR; EN_SET; DataPort= Data; _nop_();EN_CLR;}/*------------------------------------------------清屏函數(shù)------------------------------------------------*/void LCD_Clear(void) { LCD_Write_Com(0x01); DelayMs(5);}/*------------------------------------------------寫入字符串函數(shù)------------------------------------------------*/void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) { if (y == 0) { LCD_Write_Com(0x80 + x); //表示第一行}else { LCD_Write_Com(0xC0 + x); //表示第二行} while (*s) { LCD_Write_Data( *s); s ++; }}
評論