标签:
1 #include <htc.h> 2 //#define FOSC_HS 0xFFFE 3 4 #define _XTAL_FREQ 20000000 //20MHz晶振 5 6 #define CONVST RA0 7 #define BUSY RA1 8 #define SDATA RA2 9 #define SCLK RA3 10 #define TXD RB1 11 12 //////////////Prototypes//////////////// 13 14 unsigned char readfromad7894(void); 15 void sendbyte(unsigned char value); 16 void delay(unsigned char count); 17 18 /////////////////////////////////////// 19 20 21 unsigned char readfromad7894(void) 22 { 23 24 char i; 25 unsigned int X; 26 CONVST=0; 27 delay(100); 28 CONVST=1; 29 delay(100); 30 X=0; 31 SCLK=0; 32 for(i=0;i<16;i++) 33 { 34 35 SCLK=1; 36 delay(100); 37 X=X+1; 38 SCLK=0; 39 delay(100); 40 TXD=SDATA; 41 42 } 43 44 delay(100); 45 return(X); 46 47 } 48 49 /*void sendbyte(unsigned char value) 50 { 51 52 unsigned char j; 53 TXD=0; //先传高位 54 delay(100); 55 for(j=0;j<8;j++) 56 { 57 58 TXD=(value&0x01); //再传低位 59 delay(100); 60 value>>=1; 61 62 } 63 TXD=1; //发送结束标志位 64 delay(100); 65 66 }*/ 67 68 ////////////////////Main//////////////////// 69 70 void main(void) 71 { 72 73 TRISA=0xF6; //RA0,RA3 输出,RA1,RA2 输入 74 TRISB=0x00; //RB 输出 75 OPTION_REG=0x07; //TMR0 76 CONVST=1; 77 TXD=0; 78 delay(100); 79 unsigned char Y; 80 ADCON1=0x06; 81 CONVST=1; 82 delay(100); 83 SCLK=0; 84 delay(100); 85 while(1) 86 { 87 88 //TXD=0; 89 Y=readfromad7894(); 90 //NOP(); 91 //sendbyte(Y); 92 //sendbyte(Y>>8); 93 delay(100); 94 95 } 96 97 } 98 99 /////////////////Delay////////////////// 100 101 void delay(unsigned char count) 102 { 103 104 unsigned char k; 105 for(k=0;k<count;k++); 106 NOP(); 107 108 }
标签:
原文地址:http://www.cnblogs.com/loves6036/p/5690132.html