|
|||||||||||
| 技術交流 | 電路欣賞 | 工控天地 | 數(shù)字廣電 | 通信技術 | 電源技術 | 測控之家 | EMC技術 | ARM技術 | EDA技術 | PCB技術 | 嵌入式系統(tǒng) 驅動編程 | 集成電路 | 器件替換 | 模擬技術 | 新手園地 | 單 片 機 | DSP技術 | MCU技術 | IC 設計 | IC 產業(yè) | CAN-bus/DeviceNe |
貼一個控制數(shù)字電位器X9241的C程序,偌覺得好用就頂一下! |
| 作者:yonghuang 欄目:單片機 |
//調試電位器X9241的程序 //硬件連接 //PE2-----SDA //PE3-----SCL //使用超級終端能看到顯示的結果,我這里用來控制AD的放大倍數(shù) //通過改變數(shù)字電位器的阻值,來改變放大倍數(shù)。 //作者:黃勇 huangyongbj123@eyou.com //ICC-AVR application builder : 2006-9-12 14:32:16 // Target : M128 // CRYSTAL: 11.059MHZ #include <iom128v.h> #include <macros.h> #define uCHAR unsigned CHAR #define uint unsigned int union { uCHAR TEMP_ad[2]; uint int_ad; }ad; //ADC initialize // Conversion time: 75uS void adc_init(void) { ADCSRA = 0x00; //disable adc ADMUX = 0x40; //SELECT adc input 0 ACSR = 0x80; ADCSRA = 0xE6; } void I2c_wait(uCHAR cnt) { uCHAR i,j; for(j=0;j<cnt;j++) for(i=0;i<100;i++); } void I2c_start(void) { DDRE=0X0C; //PE2-SDA PE3-SCL OUTPUT PORTE|=BIT(PE2); //SDA=1; PORTE|=BIT(PE3); //SCL=1; I2c_wait(5); PORTE&=~BIT(PE2); //SDA=0; I2c_wait(5); PORTE&=~BIT(PE3); //SCL=0; } void I2c_stop(void) { DDRE=0X0C; //PE2-SDA PE3-SCL OUTPUT PORTE&=~BIT(PE2); //SDA=0; I2c_wait(5); PORTE|=BIT(PE3); //SCL=1; I2c_wait(5); PORTE|=BIT(PE2); //SDA=1; } uCHAR I2c_sendbyte(uCHAR udata) { uCHAR i,ack; DDRE=0X0C; for(i=0;i<8;i++) { if((udata&0x80)==0x80) PORTE|=BIT(PE2); //SDA=1; else PORTE&=~BIT(PE2); //SDA=0; udata<<=1; I2c_wait(5); PORTE|=BIT(PE3); //SCL=1; I2c_wait(5); PORTE&=~BIT(PE3); //SCL=0; I2c_wait(5); } PORTE|=BIT(PE2); //SDA=1; //接受ACK信號 DDRE=0X08; I2c_wait(5); PORTE|=BIT(PE3); //SCL=1; I2c_wait(5); if((PINE&0X04)==0x04) ack=0x01; else ack=0x00; // DDRE|=0X0C; PORTE&=~BIT(PE3); //SCL=0; I2c_wait(5); return (ack); } uCHAR I2c_readbyte(void) { uCHAR i,TEMP; DDRE=0X08; for(i=0;i<8;i++) { PORTE|=BIT(PE3); //SCL=1; I2c_wait(5); TEMP<<=1; if(PINE&0X04) TEMP|=0x01; PORTE&=~BIT(PE3); //SCL=0; I2c_wait(5); } return (TEMP); } void delay_ms(uCHAR m) { unsigned int j; for(;m>0;m--) { for(j=0;j<1000;j++); } } void port_init(void)//IN-0X00 OUT-0XFF; { PORTA = 0x00; DDRA = 0x00; PORTB = 0x00; DDRB = 0xff; PORTC = 0x00; //M103 OUTPUT ONLY DDRC = 0x00; PORTD = 0x00; DDRD = 0x00; PORTE = 0xff; DDRE = 0x00; PORTF = 0x00; DDRF = 0x00; PORTG = 0x00; DDRG = 0x00; } //UART1 initialize // desired baud rate:9600 // actual baud rate:9600 (0.0%) // CHAR size: 8 bit // parity: Disabled void uart0_init(void) { UCSR0B = 0x00; //disable while setting baud rate UCSR0A = 0x00; UCSR0C = 0x06; UBRR0L = 0x47; //set baud rate lo UBRR0H = 0x00; //set baud rate hi UCSR0B = 0x18; } //UART1 initialize // desired baud rate:2400 // actual invalid baud rate // CHAR size: 8 bit // parity: Disabled void uart1_init(void) { UCSR1B = 0x00; //disable while setting baud rate UCSR1A = 0x00; UCSR1C = 0x06; UBRR1L = 0x00 /*INVALID SETTING*/; //set baud rate lo UBRR1H = 0x00 /*INVALID SETTING*/; //set baud rate hi UCSR1B = 0x18; } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts XDIV = 0x00; //XTAL divider XMCRA = 0x04; //external MEMORY port_init(); adc_init(); uart0_init(); uart1_init(); MCUCR = 0x40; EICRA = 0x00; //extended ext ints EICRB = 0x00; //extended ext ints EIMSK = 0x00; TIMSK = 0x00; //timer interrupt sources ETIMSK = 0x00; //extended timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized } void Uart_sendCHAR( unsigned CHAR data ) { while ( !( UCSR0A & (1<<UDRE0)) ); UDR0 = data; } unsigned CHAR Uart_getCHAR( void ) { while ( !(UCSR0A & (1<<RXC0)) ); return UDR0; } void putstring(uCHAR *s) { while(*s!='\0') { Uart_sendCHAR(*s); s++; } } void sendnum(unsigned CHAR num) { unsigned CHAR i; if(num>=100) { i=num/100+0x30; Uart_sendCHAR(i); |
| 2樓: | >>參與討論 |
| 作者: yonghuang 于 2006/10/31 16:02:00 發(fā)布:
感覺不是很爽 其實這個程序很經(jīng)典的,對初學AVR單片機可謂是大有益處: 1.有作為基礎和調試用的串口程序 2.有很標準的I2C協(xié)議(AVR版) 3.有ATEMG128自己內部讀取10位AD的用法 4.有串口輸出的高級用法,包括輸出單個字節(jié)、10進制數(shù)、以及浮點型數(shù)。 這個程序WWW.18IC.COM的版主還特意邀請轉貼到他的論壇上去。 |
|
| 3樓: | >>參與討論 |
| 作者: shiyang800 于 2006/11/1 11:17:00 發(fā)布:
頂一下! |
|
| 4樓: | >>參與討論 |
| 作者: cuihaijun 于 2006/11/5 0:10:00 發(fā)布:
頂一下 |
|
| 5樓: | >>參與討論 |
| 作者: yonghuang 于 2006/11/15 9:03:00 發(fā)布:
頂一下! |
|
| 6樓: | >>參與討論 |
| 作者: 樺仔 于 2009/6/1 21:30:56 發(fā)布:
給個完整的程序啊~~~ |
|
| 7樓: | >>參與討論 |
| 作者: 麻煩 于 2009/9/4 15:34:38 發(fā)布:
怎么沒有C語言版的《《《《《 |
|
|
|
| 免費注冊為維庫電子開發(fā)網(wǎng)會員,參與電子工程師社區(qū)討論,點此進入 |
Copyright © 1998-2006 www.udpf.com.cn 浙ICP證030469號 |