DSP

AD/DA转换(程序)

2019-07-13 19:57发布

#include #include "./delay/delay.h" #define LCD_WRITE_DATA 1 #define LCD_WRITE_COM 0 #define LCDPORT P0 #define SUCC 0 #define ERR 1 sbit RS = P2^4; sbit RW = P2^5; sbit E = P2^6; sbit SCL = P1^0; sbit SDA = P1^1; bit ack = 0; void iic_start() { SDA = 1; SCL = 1; delay_us(1); SDA = 0; delay_us(1); SCL = 0; } void iic_stop() { SDA = 0; SCL = 1; delay_us(1); SDA = 1; delay_us(1); SCL = 0; } bit iic_send_byte(unsigned char byte) { unsigned char i; SDA = 1; for(i = 0;i < 8;i++) { SDA = byte & 0x80; SCL = 1; delay_us(1); SCL = 0; byte <<= 1; } SCL = 1; SDA = 1; delay_us(1); if(0 == SDA) { ack = 1; } else { ack = 0; } SCL = 0; return ack; } void iic_noack() { SDA = 1; SCL = 1; delay_us(1); SCL = 0; } void iic_ack() { SDA = 0; SCL = 1; delay_us(1); SCL = 0; } unsigned char iic_rcv_byte() { unsigned char i; unsigned char temp = 0; unsigned char a; SDA = 1; for(i = 0;i < 8;i++) { SCL = 0; delay_us(1); SCL = 1; if(SDA == 1) { a = 0x01; } else { a = 0x0; } temp |= (a<<(7-i)); delay_us(1); } SCL = 0; return temp; } unsigned char AT24C02_send_str(unsigned char devaddr,unsigned char romaddr,unsigned char *s,unsigned char num) { unsigned char i; iic_start(); iic_send_byte(devaddr); if(0 == ack) return ERR; iic_send_byte(romaddr); if(0 == ack) return ERR; for(i = 0;i < num;i++) { iic_send_byte(*s); if(0 == ack) return ERR; s++; } iic_stop(); return SUCC; } unsigned char AT24C02_rcv_byte(unsigned char devaddr,unsigned char romaddr,unsigned char *s,unsigned char num) { unsigned char i; iic_start(); iic_send_byte(devaddr); if(0 == ack) return ERR; iic_send_byte(romaddr); if(0 == ack) return ERR; iic_start(); iic_send_byte(devaddr + 1); for(i = 0;i < num - 1;i++) { *s = iic_rcv_byte(); iic_ack(); s++; } *s = iic_rcv_byte(); iic_noack(); iic_stop(); return SUCC; } void lcd1602_write_data(unsigned char byte,unsigned char flag) { if(flag) { RS = 1; } else { RS = 0; } RW = 0; E = 1; LCDPORT = byte; delay_us(5); E = 0; } void lcd_init() { delay_ms(15); lcd1602_write_data(0x38,LCD_WRITE_COM); delay_ms(5); lcd1602_write_data(0x38,LCD_WRITE_COM); delay_ms(5); lcd1602_write_data(0x38,LCD_WRITE_COM); delay_ms(5); lcd1602_write_data(0x38,LCD_WRITE_COM); delay_ms(5); lcd1602_write_data(0x08,LCD_WRITE_COM); delay_ms(5); lcd1602_write_data(0x01,LCD_WRITE_COM); delay_ms(5); lcd1602_write_data(0x06,LCD_WRITE_COM); delay_ms(5); lcd1602_write_data(0x0c,LCD_WRITE_COM); delay_ms(5); } void lcd_write_str(unsigned char x,unsigned char y,unsigned char *p) { if((x > 15) || (y > 1)) { return; } if(0 == y) { lcd1602_write_data(0x80+x,LCD_WRITE_COM); } else { lcd1602_write_data(0x80+0x40+x,LCD_WRITE_COM); } while(*p != '