

已閱讀5頁,還剩19頁未讀, 繼續(xù)免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
#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函數(shù):1、void main(void) /主函數(shù) while(1) /連續(xù)不間斷地得到AD值 ADZ = (AD值); 2、void KeyScan(void) if(K1=0) K1OK=1; 3、void T2_ISR(void) interrupt 5 /定時器,定時掃描按鍵,當有按鍵按下時,顯示ADZTF2H = 0; KeyScan(); if(K1OK=1) K1OK=0; /LCD1602第一行顯示字母“ADZ”,第二行顯示數(shù)字ADZ,24C02存儲此值*/*1602高四位LCD1602_RS=P06,LCD1602_EN=P07,DB4 = P10, DB5 = P11,DB6 = P12,DB7 = P13 AD轉換,24位精度:AIN0.0 ,Verf+接100K可調(diào)電阻測試,滿量程2500mv四個按鍵: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; / 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 24500000 / SYSCLK frequency in Hz#define MDCLK 2457600 / Modulator clock in Hz (ideal is / (2.4576 MHz)#define OWR 20 / Desired Output Word Rate in Hz ADC0輸出字速率,最大960#define BAUDRATE 115200 / 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 / 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_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觸發(fā)掃描次#define AUX1 TIMER_TICKS_PER_MS*TOGGLE_RATE#define AUX2 -AUX1#define TIMER2_RELOAD AUX2 / Reload value for Timer2 /-#define uchar unsigned char#define uint unsigned int/- -sbit lcdrs=P06; /數(shù)據(jù)/命令選擇端,低電平為指令,高電平為數(shù)? 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.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 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, this 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 then / 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 addressstatic uchar K1OK,K2OK,K3OK,K4OK;static uchar ge,shi,bai,qian; static unsigned long ADC_OutputVal; /得到的份數(shù) static unsigned long accumulator = 0;/ADC累積測量N次值的和static uint measurements = 0; / ADC測量的次數(shù)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 Port_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_ISR(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);/-/ 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; / 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 / UART0_Init(); / Initialize UART0 for printfsLcd_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 timeout 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_buff);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位數(shù)據(jù)口 delay(5); / lcden=1; / lcden=0; Lcd_1602_write(0,0x28); /顯示方式設置 Lcd_1602_write(0,0x0e); /設置開顯示,不顯示光標 Lcd_1602_write(0,0x06); /默認(0x06)寫入新數(shù)據(jù)后光標右移 屏幕不移動 Lcd_1602_write(0,0x01); /顯示清0,數(shù)據(jù)指針清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); /適當延時,數(shù)據(jù)穩(wěn)定 lcden=0; /送入數(shù)據(jù) 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); /適當延時,數(shù)據(jù)穩(wěn)定 lcden=0; /送入數(shù)據(jù) date0;i-) for(j=110;j0;j-);/-void Oscillator_Init (void) OSCICN = 0x83; / 配置內(nèi)部振蕩器 ,不分頻 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 Port_Init (void)XBR1 = 0x40; /弱上拉使能,交叉開關使能。P0MDOUT |= 0xC0; / P0.6 P0.7 pins push-pull output LCD控制信號P0MDOUT &= 0xFC; /P0.1 P0.2 置0,開漏。 SMBus時鐘和數(shù)據(jù)位P1MDOUT &= 0xF0; /P1低四位開漏,液晶驅動位P1MDOUT |= 0x20; /P1.5 推挽輸出/P1MDIN &=0X0F; /P1高四位配置為模擬輸入P1SKIP &= 0x00; /p1不被交叉開關跳過XBR0 = 0x04; / Enable SMBus pins/串口還沒配置/-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 the 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#elif (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; / 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 = 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) EIE1 |= 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-differential)/-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); /松開,連續(xù)檢測會有問題 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);Lcd_1602_write1(1,qian+0x30);delay(3);Lcd_1602_write1(1,bai+0x30);delay(3);Lcd_1602_wri
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 付費搭建店鋪合同范本
- 內(nèi)墻抹灰施工合同范本
- 農(nóng)民工工資付款合同范本
- 關于規(guī)范使用合同范本
- 辦公輔助崗位合同范本
- 住宅消防維修采購合同范本
- mind+ 星球大戰(zhàn) 教學設計
- 出售貨架合同范本
- 與勞務外包簽訂合同范本
- 勘探合同屬于合同范本
- 2024年廣東省2024屆高三高考模擬測試(一)一模 化學試卷(含答案)
- 半導體行業(yè)質(zhì)量管理與質(zhì)量控制
- 2024年山東省春季高考技能考試汽車專業(yè)試題庫-下(判斷題匯總)
- 部編版道德與法治二年級下冊第三單元 綠色小衛(wèi)士 單元作業(yè)設計
- 戲曲鑒賞完整版剖析課件
- 《幼兒園經(jīng)營與管理》課件
- 熱化學儲熱耦合高溫相變儲熱多物理場協(xié)同調(diào)控機理
- 趙匡胤:中國北宋時期的開國皇帝2
- 中國紡織服裝制造業(yè)年度授信政策指引研究報告
- 零基礎學機器學習
- 西方繪畫藝術流派(最全)課件
評論
0/150
提交評論