STC12C5A60S2单片机的A/D转换口在P1口(P1.7-P1.0),有8路10位高速A/D转换器,速度可到达250 KHz(25万次/秒)。脉宽调制(PWM)是一种运用程序来操控波形占空比、周期、相位波形的技能。STC12C5A60S2单片机的PAC模块能够经过程序设定,使其作业于8位PWM形式。
下面是一段将ADC和PWM结合起来运用的程序:
/***************************************************************时刻:2012.12.1晶振:12MHz功用描绘:AD收集电位器的电压信号,然后信号以PWM信号输出操控LED的亮度(调理电位器)当电位器两头的电压大时,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; //指令数据 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延时函数,含有输入参数 unsigned char t,无返回值unsigned char 是界说无符号字符变量,其值的规模是0~255 这儿运用晶振12M,准确延时请运用汇编,大致延时长度如下 T=tx2+5 uS ------------------------------------------------*/void DelayUs2x(unsigned char t){ while(--t);}/*------------------------------------------------mS延时函数,含有输入参数 unsigned char t,无返回值unsigned char 是界说无符号字符变量,其值的规模是0~255 这儿运用晶振12M,准确延时请运用汇编------------------------------------------------*/void DelayMs(unsigned char t){while(t--){//大致延时1mSDelayUs2x(245);DelayUs2x(245);}}/*------------------------------------------------判忙函数------------------------------------------------*/bit LCD_Check_Busy(void) { DataPort= 0xFF; RS_CLR; RW_SET; EN_CLR; _nop_(); EN_SET;return (bit)(DataPort & 0x80);}/*------------------------------------------------写入指令函数------------------------------------------------*/void LCD_Write_Com(unsigned char com) { while(LCD_Check_Busy()); //忙则等候RS_CLR; RW_CLR; EN_SET; DataPort= com; _nop_(); EN_CLR;}/*------------------------------------------------写入数据函数------------------------------------------------*/void LCD_Write_Data(unsigned char Data) { while(LCD_Check_Busy()); //忙则等候RS_SET; RW_CLR; EN_SET; DataPort= Data; _nop_();EN_CLR;}/*------------------------------------------------清屏函数------------------------------------------------*/void LCD_Clear(void) { LCD_Write_Com(0x01); DelayMs(5);}/*------------------------------------------------写入字符串函数------------------------------------------------*/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 ++; }}