📄 a2demo.c
字号:
#include <pic18.h>#include "a2demo.h"#include <stdio.h>void init(void);void putch(unsigned char);volatile unsigned char CHANNEL = 0; /* current analog input channel selected */volatile unsigned char LEDCHAN = 1; /* show which LED to light, corresponds to channel */void main(void){ unsigned char volts; unsigned char decivolts; unsigned char last_value; unsigned char last_channel=2; // to force an update at startup init(); while(1) { /* update the channel select LEDS */ PORTD=LEDCHAN; /* update A2D configuration control */ /* result is left justified */ ADCON1=(PORTC&0x0F); /* update the channel to the A2D converter */ CHS0=(CHANNEL&0b00000001); CHS1=((CHANNEL&0b00000010)>>1); CHS2=((CHANNEL&0b00000100)>>2); /* request the A2D conversion */ GODONE=1; while(GODONE)continue; ADIF=0; if((ADRESH!=last_value)||(CHANNEL!=last_channel)) { /* if update required, output the results */ /* calculate the output voltage */ volts=0; for(decivolts=(ADRESH*50/255);decivolts>=10;decivolts-=10) volts++; printf("%cChannel %d has %d.%d volts. ",0x0D,(CHANNEL&0x07),volts,decivolts); } last_value=ADRESH; last_channel=CHANNEL; }}void init(void){ RBIE=1; /* enable PORTB interrupts so that */ GIEH=1; /* pushbuttons can be used to change */ GIEL=1; /* A2D channel select */ TXEN=1; /* enable serial port transmissions */ SPEN=1; TXIE=0; /* not interrupt driven */ TRISA=0x2F; /* Some PORTA pins are analog input channels */ TRISB=0x30; /* Pushbutton switches to change channel are on PORTB */ PORTB=0; TRISD=0; /* LED panel to indicate current channel is on PORTD */ TRISC=0x0F;/* switch panel to control A2D config is on PORTC */ TRISE=0x07; /* Some PORTE inputs are analog input channels */ ADON=1; /* enable A2D converter */ ADIE=0; /* not interrupt driven */ ADCON1=0b00000000;}void putch(unsigned char c){ TXREG=c; /* transmit a character to Serial IO */ while(!TXIF)continue; TXIF=0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -