基于8051F350的AD转换显示存储程序_第1页
基于8051F350的AD转换显示存储程序_第2页
基于8051F350的AD转换显示存储程序_第3页
基于8051F350的AD转换显示存储程序_第4页
基于8051F350的AD转换显示存储程序_第5页
已阅读5页,还剩37页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include #include #include /-/ 16-bit SFR Definitions for F35x/-/*需求:1、AD转换,24位精度:AIN0.0 2、四个按键:K1=P14,K2=P15,K3=P16,K=P17 3、1602液晶屏显示AD值:LCD1602_RS=P06,LCD1602_EN=P07,DB4 = P10, DB0=DB5 = P11,DB6 = P12,DB7 = P13 4、存储(24C02):SDA=P00,SCL=P01函数:1、void main(void) /主函数 while(1) /连续不间断地得到AD值 ADZ = (AD值);

2、 2、void KeyScan(void) if(K1=0) K1OK=1; 3、void T2_ISR(void) interrupt 5 /定时器,定时扫描按键,当有按键按下时,显示ADZ TF2H = 0; KeyScan(); if(K1OK=1) K1OK=0; /LCD1602第一行显示字母“ADZ”,第二行显示数字ADZ,24C02存储此值*/*1602高四位LCD1602_RS=P06,LCD1602_EN=P07,DB4 = P10, DB5 = P11,DB6 = P12,DB7 = P13 AD转换,24位精度:AIN0.0 ,Verf+接100K可调电阻测试,满量程25

3、00mv四个按键:K1=P14,K2=P15,K3=P16,K=P17存储(24C02):SDA=P00,SCL=P01 (和T2扫描按键有冲突,应该实时存储)*/Routinessfr16 ADC0DEC = 0x9A; / ADC0 Decimation Ratio Registersfr16 TMR3RL = 0x92; / Timer3 reload registerssfr16 TMR3 = 0x94; / Timer3 counter registerssfr16 TMR2RL = 0xCA; / Timer2 Reload Registersfr16 TMR2 = 0xCC; /

4、 Timer2 Registersbit SDA = P00; / SMBus on P0.0sbit SCL = P01; / and P0.1sbit K1=P14;/四个按键sbit K2=P15;sbit K3=P16;sbit K4=P17;/-/ Global CONSTANTS/-/-#define SYSCLK / SYSCLK frequency in Hz#define MDCLK / Modulator clock in Hz (ideal is / (2.4576 MHz)#define OWR 20 / Desired Output Word Rate in Hz A

5、DC0输出字速率,最大960#define BAUDRATE / Baud rate of UART in bps /-#define SMB_FREQUENCY 50000 / Target SCL clock rate / This example supports between 10kHz / and 100kHz#define WRITE 0x00 / SMBus WRITE command#define READ 0x01 / SMBus READ command#define EEPROM_ADDR 0xA0 / Device address for slave target /

6、 Note: This address is specified / in the Microchip 24LC02B / datasheet./ SMBus Buffer Size#define SMB_BUFF_SIZE 0x08 / Defines the maximum number of bytes / that can be sent or received in a / single transfer/ Status vector - top 4 bits only#define SMB_MTSTA 0xE0 / (MT) start transmitted#define SMB

7、_MTDB 0xC0 / (MT) data byte transmitted#define SMB_MRDB 0x80 / (MR) data byte received /- /-定时器2常量#define TOGGLE_RATE 1000 /每50ms检测按键1次#define TIMER_TICKS_PER_MS SYSCLK/8/1000 /定时器每ms触发扫描次#define AUX1 TIMER_TICKS_PER_MS*TOGGLE_RATE#define AUX2 -AUX1#define TIMER2_RELOAD AUX2 / Reload value for Timer

8、2 /-#define uchar unsigned char#define uint unsigned int/- -sbit lcdrs=P06; /数据/命令选择端,低电平为指令,高电平为数? sbit lcden=P07; /- uchar D1,D2,D3,D4,D5,D6,D7,D8;uchar* pSMB_DATA_IN; / Global pointer for SMBus data / All receive data is written hereuchar SMB_SINGLEBYTE_OUT; / Global holder for single byte writes

9、.uchar* pSMB_DATA_OUT; / Global pointer for SMBus data. / All transmit data is read from hereuchar SMB_DATA_LEN; / Global holder for number of bytes / to send or receive in the current / SMBus transfer.uchar WORD_ADDR; / Global holder for the EEPROM word / address that will be accessed in / the next

10、 transferuchar TARGET; / Target SMBus slave addressbit SMB_BUSY = 0; / Software flag to indicate when the / EEPROM_ByteRead() or / EEPROM_ByteWrite() / functions have claimed the SMBusbit SMB_RW; / Software flag to indicate the / direction of the current transferbit SMB_SENDWORDADDR; / When set, thi

11、s flag causes the ISR / to send the 8-bit / after sending the slave address.bit SMB_RANDOMREAD; / When set, this flag causes the ISR / to send a START signal after sending / the word address. / For the 24LC02B EEPROM, a random read / (a read from a particular address in / memory) starts as a write t

12、hen / changes to a read after the repeated / start is sent. The ISR handles this / switchover if the / bit is set.bit SMB_ACKPOLL; / When set, this flag causes the ISR / to send a repeated START until the / slave has acknowledged its address static uchar K1OK,K2OK,K3OK,K4OK;static uchar ge,shi,bai,q

13、ian; static unsigned long ADC_OutputVal; /得到的份数 static unsigned long accumulator = 0;/ADC累积测量N次值的和static uint measurements = 0; / ADC测量的次数static unsigned long result=0;uint i,k;uchar num;uchar code table1= ADZ: ;uchar code table2=test data.;/-/ Function PROTOTYPES/-void Oscillator_Init (void);void P

14、ort_Init (void);void UART0_Init (void);void ADC0_Init(void);void delay(uint x);void Lcd_1602_init();void Lcd_1602_write(uchar com,date);void Lcd_1602_write1();void SMBus_Init(void);void Timer0_Init(void);void Timer3_Init(void);void SMBus_ISR(void);void Timer2_Init(void);void KeyScan();void Timer3_IS

15、R(void);void EEPROM_ByteWrite(unsigned char addr, unsigned char dat);void EEPROM_WriteArray(unsigned char dest_addr, unsigned char* src_addr, unsigned char len);unsigned char EEPROM_ByteRead(unsigned char addr);void EEPROM_ReadArray(unsigned char* dest_addr, unsigned char src_addr, unsigned char len

16、);/-/ MAIN Routine/-void main (void)char in_buff11 = 0; PCA0MD &= 0x40; / WDTE = 0 (clear watchdog timer / enable) Oscillator_Init(); / Initialize system clock while(!SDA) / Provide clock pulses to allow the slave to advance out / of its current state. This will allow it to release SDA. XBR1 = 0x40;

17、 / Enable Crossbar SCL = 0; / Drive the clock low for(i = 0; i 255; i+); / Hold the clock low SCL = 1; / Release the clock while(!SCL); / Wait for open-drain / clock output to rise for(i = 0; i 10; i+); / Hold the clock high XBR1 = 0x00; / Disable Crossbar Port_Init(); / Initialize Crossbar and GPIO

18、 / UART0_Init(); / Initialize UART0 for printfs Lcd_1602_init(); ADC0_Init(); / Initialize ADC0 AD0INT = 0; ADC0MD = 0x83; / Start continuous conversions Timer0_Init (); / Configure Timer0 for use as SMBus Timer2_Init (); / clock source Timer3_Init (); / Configure Timer3 for use with SMBus / low tim

19、eout detect SMBus_Init (); / Configure and enable SMBus EIE1 |= 0x01; / Enable the SMBus interruptEA = 1; / Enable global interrupts/加入按键测试-/*EEPROM_WriteArray(0x50, table2, sizeof(table2);EEPROM_ReadArray(in_buff, 0x50, sizeof(in_buff);while(K1=0)void Lcd_1602_init();delay(3);for(i=0;isizeof(in_buf

20、f);i+) Lcd_1602_write(1,in_buffi);delay(3); /- */ while (1) / Spin forever /-/ Initialization Subroutines/-/- -/1602程序部分void Lcd_1602_init() Lcd_1602_write(0,0x28); /0x28=0010 1000,将液晶设置为16*2显示,5*7点阵,4位数据口 delay(5); / lcden=1; / lcden=0; Lcd_1602_write(0,0x28); /显示方式设置 Lcd_1602_write(0,0x0e); /设置开显示

21、,不显示光标 Lcd_1602_write(0,0x06); /默认(0x06)写入新数据后光标右移 屏幕不移动 Lcd_1602_write(0,0x01); /显示清0,数据指针清0 void Lcd_1602_write(uchar com,date) uchar i; uchar temp; lcdrs=com; /将形参传给lcdrs for(i=0;i4); /将date的 高四位送入P1第四位,同时保持P1的高 四位不变 /如果使用P1的低四位的话,则该处应该改为:P1=(P1&0xf0)|(date4)&0x0f); delay(3); /适当延时,数据稳定 lcden=0;

22、/送入数据 date=4; /低四位左移到高四位 void Lcd_1602_write1(uchar com,date) uchar i; uchar temp; lcdrs=com; /将形参传给lcdrs for(i=0;i4); /将date的 高四位送入P1第四位,同时保持P1的高 四位不变 /如果使用P1的低四位的话,则该处应该改为:P1=(P1&0xf0)|(date4)&0x0f); delay(3); /适当延时,数据稳定 lcden=0; /送入数据 date0;i-) for(j=110;j0;j-);/-void Oscillator_Init (void) OSCIC

23、N = 0x83; / 配置内部振荡器 ,不分频 RSTSRC = 0x04; / Enable missing clock detector/-/ Port_Init/-/ Return Value : None/ Parameters : None/ThisfunctioninitializestheGPIO and the Crossbar 初始化GPI0和交叉开关/ Pinout:/ P0.4 - UART TX (digital, push-pull) / P0.5 - UART RX (digital, open-drain)/ AIN0.2 - ADC0 input/-void

24、Port_Init (void)XBR1 = 0x40; /弱上拉使能,交叉开关使能。 P0MDOUT |= 0xC0; / P0.6 P0.7 pins push-pull output LCD控制信号P0MDOUT &= 0xFC; /P0.1 P0.2 置0,开漏。 SMBus时钟和数据位P1MDOUT &= 0xF0; /P1低四位开漏,液晶驱动位P1MDOUT |= 0x20; /P1.5 推挽输出/P1MDIN &=0X0F; /P1高四位配置为模拟输入P1SKIP &= 0x00; /p1不被交叉开关跳过XBR0 = 0x04; / Enable SMBus pins/串口还没配

25、置/-void SMBus_Init (void) /SMB0CF = 0x40; / Use Timer0 overflows as SMBus clock SMB0CF=0x5C; / source; / Disable slave mode; / Enable setup & hold time extensions; / Enable SMBus Free timeout detect; / Enable SCL low timeout detect; SMB0CF |= 0x80; / Enable SMBus;void Timer0_Init (void)/ Make sure t

26、he Timer can produce the appropriate frequency in 8-bit mode/ Supported SMBus Frequencies range from 10kHz to 100kHz. The CKCON register/ settings may need to change for frequencies outside this range.#if (SYSCLK/SMB_FREQUENCY/3) 255) #define SCALE 1 CKCON |= 0x04; / Timer0 clock source = SYSCLK#eli

27、f (SYSCLK/SMB_FREQUENCY/4/3) 255) #define SCALE 4 CKCON |= 0x01; CKCON &= 0x06; / Timer0 clock source = SYSCLK / 4#endif TMOD = 0x02; / Timer0 in 8-bit auto-reload mode !注意综合设置 TH0 = -(SYSCLK/SMB_FREQUENCY/12/3); / Timer0 configured to overflow at 1/3 / the rate defined by SMB_FREQUENCY TL0 = TH0; /

28、 Init Timer0 TR0 = 1; / Timer0 enabledvoid Timer2_Init(void) CKCON &= 0x60; / TMR2CN &= 0x01; TMR2CN = 0x00; / TMR2RL = -(SYSCLK/2/40); / Reload value to be used in Timer2 50ms TMR2RL = TIMER2_RELOAD; TMR2 = TMR2RL; / Init the Timer2 register TMR2CN = 0x04; / Enable Timer2 in auto-reload mode ET2 =

29、1; / Timer2 interrupt enabledvoid Timer3_Init (void) TMR3CN = 0x00; / Timer3 configured for 16-bit auto- / reload, low-byte interrupt disabled CKCON &= 0x40; / Timer3 uses SYSCLK/12 TMR3RL = -(SYSCLK/12/40); / Timer3 configured to overflow after TMR3 = TMR3RL; / 25ms (for SMBus low timeout detect) E

30、IE1 |= 0x80; / Timer3 interrupt enable TMR3CN |= 0x04; / Start Timer3/-/ UART0_Init/-/ Return Value : None/ Parameters : None/ Configure the UART0 using Timer1, for and 8-N-1./-/-/ ADC0_Init/-/ Return Value : None/ Parameters : None/ Initialize the ADC to use the temperature sensor. (non-differentia

31、l)/-void KeyScan() Lcd_1602_write1(0,0x80); delay(3); Lcd_1602_write1(1,t);delay(3);Lcd_1602_write1(1,e);delay(3);Lcd_1602_write1(1,s);delay(3);Lcd_1602_write1(1,t);delay(3); if(K1 = 0) delay(10);if(K1=0)/ 有效按键K1OK=1;/while(!K1); /松开,连续检测会有问题 void Timer2_ISR (void) interrupt 5 TF2H = 0; / Reset Interrupt KeyScan(); / K1OK=1; if(K1OK=1) K1OK=0; for(num=0;num8;num+) Lcd_1602_write1(1,table1num); delay(3); Lcd_1602_write1(0,0xC0);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论