//晟矽微单片机编程第八天流水灯显示AD//qq:74145445//快手:共同学习stm8/**************************************************/// MC32P7311// _____@_____// led11--p16|1 16|p17--anjian// vdd|2 15|vss// led10--p15|3 14|p00--led0// led9--p14|4 13|p01--led1// led8--p13|5 12|p02--led5// led7--p12|6 11|p03--led4// led6--p11|7 10|p04--led3// ad --p10|8_________9|p05--led2/**************************************************/#define bit0 0b00000001#define bit1 0b00000010#define bit2 0b00000100#define bit3 0b00001000#define bit4 0b00010000#define bit5 0b00100000#define bit6 0b01000000#define bit7 0b10000000#define bit8 0b0000000100000000#define bit9 0b0000001000000000#define bit10 0b0000010000000000#define bit11 0b0000100000000000#define bit12 0b0001000000000000#define bit13 0b0010000000000000#define bit14 0b0100000000000000#define bit15 0b1000000000000000/**************************************************/#define led0 P00D#define led1 P01D#define led2 P05D#define led3 P04D#define led4 P03D#define led5 P02D#define led6 P11D#define led7 P12D#define led8 P13D#define led9 P14D#define led10 P15D#define led11 P16D#define led_0_oe P00OE#define led_1_oe P01OE#define led_2_oe P05OE#define led_3_oe P04OE#define led_4_oe P03OE#define led_5_oe P02OE#define led_6_oe P11OE#define led_7_oe P12OE#define led_8_oe P13OE#define led_9_oe P14OE#define led_10_oe P15OE#define led_11_oe P16OE/**************************************************/#include"MC32P7311.h"void io_chushihua(); //io初始化void ad_chushihua(); //ad初始化void xianshi(); //显示void yanshi(); //延时unsigned int xianshi_a1;void main(){unsigned int a1;//unsigned int a2;io_chushihua(); //io初始化ad_chushihua();for(;;){a1 =0;ADEOC =0; //启动AD转换while(!ADEOC);a1 =ADRH;a1 <<=4; //左移4a1 |=ADRL;xianshi_a1 =a1;xianshi();yanshi();}}/**************************************************/void io_chushihua() //io初始化{led_0_oe =1; //输出口led_1_oe =1;led_2_oe =1;led_3_oe =1;led_4_oe =1;led_5_oe =1;led_6_oe =1;led_7_oe =1;led_8_oe =1;led_9_oe =1;led_10_oe =1;led_11_oe =1;}/**************************************************/void ad_chushihua() //ad初始化{P10ANS =1; //模拟输入//ADCR0ADCHS3 =0;ADCHS2 =1;ADCHS1 =0;ADCHS0 =1; //数模转换器通道5ADLEN =1; //adc位数12位//ADEOC //1AD转结束0启动AD转换ADON =1; //使能ADC//ADCR1ADCKS2 =1;ADCKS1 =1;ADCKS0 =1; //转换时钟125kADTST =0; //参考电压输出RESS0 =0; //VREF3K上拉VRS2 =0;VRS1 =1;VRS0 =1; //内部参考VDD//ADCR2ADSMP3 =1;ADSMP2 =1;ADSMP1 =1;ADSMP0 =1; //固定1111//ADRH //高八位(11-4)只读//ADRL //低四位(3-0)只读}/**************************************************/void xianshi() //显示{if(xianshi_a1 & bit0){led0 =1;} else{led0 =0;}if(xianshi_a1 & bit1){led1 =1;} else{led1 =0;}if(xianshi_a1 & bit2){led2 =1;} else{led2 =0;}if(xianshi_a1 & bit3){led3 =1;} else{led3 =0;}if(xianshi_a1 & bit4){led4 =1;} else{led4 =0;}if(xianshi_a1 & bit5){led5 =1;} else{led5 =0;}if(xianshi_a1 & bit6){led6 =1;} else{led6 =0;}if(xianshi_a1 & bit7){led7 =1;} else{led7 =0;}if(xianshi_a1 & bit8){led8 =1;} else{led8 =0;}if(xianshi_a1 & bit9){led9 =1;} else{led9 =0;}if(xianshi_a1 & bit10){led10 =1;} else{led10 =0;}if(xianshi_a1 & bit11){led11 =1;} else{led11 =0;}}/**************************************************/void yanshi() //延时{unsigned int x;x =1000;while(x){x--;}}/**************************************************//**************************************************/
|