[ บทความ : ตัวอย่างสำหรับ AVR ตอนที่ 11 ] ทดสอบใช้งาน EEPROM กับ CP-jrAVR |
ตัวอย่างซอร์สโค้ดสำหรับติดต่อกับ EEPROM เบอร์ 24C256 ของบอร์ด CP-jrAVR เป็นดังนี้
;*************************************************************************** ;* APPLICATION NOTE FOR THE CP-JRAVR * ;* * ;* Control Board :CP-JRAVR * ;* File Name :"24256.ASM" * ;* Title :SERIAL EEPROM (24LC256) TO RS232 * ;* :9600,N,8,1 * ;* Date :25/02/43 * ;* Version :1.0 * ;* Support telephone :712 1120-1 (ETT) * ;* Support fax :3917216 (ETT) * ;* Support www :www.etteam.com,www.ett.co.th * ;* Target MCU :AT90S2313/AT90LS2313 * ;*************************************************************************** .include "2313def.inc" .ORG $0000 RJMP RESET_VEC ;Reset Handle .EQU PORT_EE = PORTD .EQU DDR_EE = DDRD .EQU PIN_EE = PIND .EQU BIT_SCL = 5 .EQU BIT_SDA = 6 .DEF TEMP = R19 .DEF TEMP1 = R20 RESET_VEC: LDI TEMP,LOW(RAMEND) OUT SPL,TEMP ;init Stack Pointer Low LDI TEMP,HIGH(RAMEND) OUT SPL+1,TEMP ;init Stack Pointer High RCALL DELAY ;delay for start CLI ;clear golbal interrupt CBI UCR,RXCIE ;clesr rx interrupt CBI UCR,TXCIE ;clesr tx interrupt CBI UCR,UDRIE ;clesr data empty interrupt CBI UCR,CHR9 ;send 8 data LDI R17,51 OUT UBRR,r17 ;baud 9600 at 8MHz Xtal SBI UCR,TXEN ;set pin tx as serial TX SBI UCR,RXEN ;set pin rx as serial RX SBI DDR_EE,BIT_SDA SBI DDR_EE,BIT_SCL SBI PORT_EE,BIT_SDA SBI PORT_EE,BIT_SCL LDI TEMP,0X0C RCALL TX232 LDI ZH,high(TEXT_TAB*2) LDI ZL,low(TEXT_TAB*2) XX: LPM LDI R16,0X00 CP R0,R16 BREQ YY MOV TEMP,R0 RCALL TX232 ADIW ZL,1 RJMP XX YY: LDI TEMP,0X0A RCALL TX232 LDI TEMP,0X0D RCALL TX232 LDI ZH,high(TEXT_TAB1*2) LDI ZL,low(TEXT_TAB1*2) AA: LPM LDI R16,0X00 CP R0,R16 BREQ BB MOV TEMP,R0 RCALL TX232 ADIW ZL,1 RJMP AA BB: LDI TEMP,0X0A RCALL TX232 LDI TEMP,0X0D RCALL TX232 LDI ZH,high(TEXT_TAB2*2) LDI ZL,low(TEXT_TAB2*2) CC: LPM LDI R16,0X00 CP R0,R16 BREQ DD MOV TEMP,R0 RCALL TX232 ADIW ZL,1 RJMP CC DD: RCALL WRITE_BYTE RCALL RANDOM_READ PUSH TEMP SWAP TEMP ANDI TEMP,0X0F RCALL HEXASC RCALL TX232 POP TEMP ANDI TEMP,0X0F RCALL HEXASC RCALL TX232 WAIT: RJMP WAIT ;**************************************************** ;* WRITE BYTE * ;**************************************************** WRITE_BYTE: CBI PORT_EE,BIT_SDA ;start condition CBI PORT_EE,BIT_SCL LDI TEMP,0XA0 ;write command RCALL TX_BYTE RCALL ACKNOW ;wait acknowledge BRCS WRITE_BYTE LDI TEMP,0X00 ;address high RCALL TX_BYTE RCALL ACKNOW ;wait acknowledge BRCS WRITE_BYTE LDI TEMP,0X00 ;address low RCALL TX_BYTE RCALL ACKNOW BRCS WRITE_BYTE LDI TEMP,0X43 ;data RCALL TX_BYTE RCALL ACKNOW ;wait acknowledge BRCS WRITE_BYTE CBI PORT_EE,BIT_SDA SBI PORT_EE,BIT_SCL ;stop condition SBI PORT_EE,BIT_SDA RCALL DELAY RET ;***************************************************** ;* RANDOM READ * ;***************************************************** RANDOM_READ: CBI PORT_EE,BIT_SDA ;start condition CBI PORT_EE,BIT_SCL LDI TEMP,0XA0 ;write command RCALL TX_BYTE RCALL ACKNOW ;wait acknowledge BRCS RANDOM_READ LDI TEMP,0X00 ;address high RCALL TX_BYTE RCALL ACKNOW ;wait acknowledge BRCS RANDOM_READ LDI TEMP,0X00 ;address low RCALL TX_BYTE RCALL ACKNOW BRCS RANDOM_READ SBI PORT_EE,BIT_SCL ;stop condition SBI PORT_EE,BIT_SDA CBI PORT_EE,BIT_SDA CBI PORT_EE,BIT_SCL LDI TEMP,0XA1 RCALL TX_BYTE RCALL ACKNOW BRCS RANDOM_READ RCALL RX_BYTE SBI PORT_EE,BIT_SCL SBI PORT_EE,BIT_SCL CBI PORT_EE,BIT_SDA SBI PORT_EE,BIT_SCL ;stop condition SBI PORT_EE,BIT_SDA RET ;***************************************************** ;* CHECK BIT ACKNOWLEDGE BIT * ;CARRY = 1: FAIL * ;CARRY = 0: PASS * ;***************************************************** ACKNOW: CBI DDR_EE,BIT_SDA ;BIT_SDA input port SBI PORT_EE,BIT_SDA SEC SBI PORT_EE,BIT_SCL SBIS PIN_EE,BIT_SDA CLC CBI PORT_EE,BIT_SCL SBI DDR_EE,BIT_SDA ;BIT_SDA output port RET ;**************************************************** ;* TX 1 BITE TO 24256 * ;**************************************************** TX_BYTE: LDI TEMP1,0X08 TX_BYTE1: SBI PORT_EE,BIT_SDA LSL TEMP BRCS TX_BYTE2 CBI PORT_EE,BIT_SDA TX_BYTE2: SBI PORT_EE,BIT_SCL CBI PORT_EE,BIT_SCL DEC TEMP1 BRNE TX_BYTE1 RET ;**************************************************** ;* RX 1 BYTE FROM 24256 * ;**************************************************** RX_BYTE: CBI DDR_EE,BIT_SDA SBI PORT_EE,BIT_SDA LDI TEMP,0X00 LDI TEMP1,0X08 RX_BYTE1: SBI PORT_EE,BIT_SCL SEC SBIS PIN_EE,BIT_SDA CLC ROL TEMP CBI PORT_EE,BIT_SCL DEC TEMP1 BRNE RX_BYTE1 SBI DDR_EE,BIT_SDA RET ;*************************************************** ;* DELAY TIME FOR START * ;*************************************************** DELAY: PUSH R26 PUSH R27 LDI R27,0XFF DEL2: LDI R26,0XFF DEL1: DEC R26 BRNE DEL1 DEC R27 BRNE DEL2 POP R27 POP R26 RET HEXASC: LDI ZL,LOW(ASC_TAB*2) LDI ZH,HIGH(ASC_TAB*2) ADD ZL,TEMP LDI TEMP,0 ADC ZH,TEMP LPM MOV TEMP,R0 RET ASC_TAB: .DB 0X30,0X31,0X32,0X33,0X34,0X35 .DB 0X36,0X37,0X38,0X39,0X41,0X42 .DB 0X43,0X44,0X45,0X46 TX232: SBIS USR,UDRE ;wait until UDRE = 1 RJMP TX232 OUT UDR,TEMP ;send data to serial RET TEXT_TAB: .DB "TEST SERIAL EEPROM(24LC256)",00 TEXT_TAB1: .DB "WRITE = 43",00 TEXT_TAB2: .DB "READ = ",00สามารถ download ไฟล์ตัวอย่างของบอร์ด พร้อม assembler ได้เลยครับ