[ บทความ : ตัวอย่างสำหรับ AVR ตอนที่ 1 ] ทดสอบ ET-AVR V1

ซอร์สโค้ดสำหรับทดสอบการทำงานของบอร์ด V1 มีรายละเอียดดังนี้ ...

	;***************************************************************************
	;* A P P L I C A T I O N   N O T E   F O R   T H E   A V R   F A M I L Y   *
	;*                                                                         *
	;* Number		:AVR                                               *
	;* File Name		:"TestV1.asm"                                        *
	;* Title		:READ ADC chanel 1 to RS232                        * 
	;* Date                 :2/3/42                                            *
	;* Version              :1.0                                               *
	;* Support telephone	:712 1120-1 (ETT)                                  *
	;* Support fax		:3917216 (ETT)                                     *
	;* Support www          :www.etteam.com,www.ett.co.th                      *
	;* Target MCU		:AT90S8535/AT90LS8535                              *
	;*                                                                         *
	;***************************************************************************
	;
	.include "8535def.inc"
	
		    .org   $0000 
	             rjmp  RESET	        ;Reset Handle
	
	;****************************************************************************
	;*                                                                          *
	;*                        Test/Example Program                              * 
	;*                                                                          *
	;****************************************************************************
	;
	.def	    temp  = r19   
	;
	RESET:      ldi     temp,low(RAMEND)
	            out     SPL,temp           ;init Stack Pointer Low     
	
	            ldi     temp,high(RAMEND)
	            out     SPL+1,temp         ;init Stack Pointer High
	
	            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 
	       
	            ldi     r17,0x00           ;set porta as Hi impledance
	            out     DDRA,r17            
	            out     PORTA,r17   
	            
	            cbi     ADCSR,ADFR         ;select single conversion     
	
	            rcall   sendch
	xx:         rjmp    xx           
	
	
	;***************************************************
	;*           program  read data for    ADC         *  
	;***************************************************
	ATOD:       in      r21,ADCL           ;read low byte
	            in      r22,ADCH           ;read high byte
	            mov     r23,r21 
	                               
	            andi    r22,0x03           ;detect 3 bit low of ADCH 
	            ldi     ZH,high(HEXTBL*2)  ;get high of pointer 
	            ldi     ZL,low(HEXTBL*2)   ;get low of pointer  
	            add     r30,r22          
	            ldi     r22,0
	            adc     r31,r22          
	            lpm                        ;get data from program memory 
	            mov     r20,r0             ;put data to rs232
	            rcall   TX232
	
	            swap    r21                
	            andi    r21,0x0F           ;detect 4 bit high of ADCL 
	            ldi     ZH,high(HEXTBL*2)  ;get high of pointer
	            ldi     ZL,low(HEXTBL*2)   ;get low of pointer  
	            add     r30,r21
	            ldi     r21,0
	            adc     r31,r21          
	            lpm                        ;get data from program memory
	            mov     r20,r0             ;put data to rs232
	            rcall   TX232
	                         
	            andi    r23,0x0F           ;detect 4 bit low of ADCL
	            ldi     ZH,high(HEXTBL*2)  ;get high of pointer
	            ldi     ZL,low(HEXTBL*2)   ;get low of pointer  
	            add     r30,r23
	            ldi     r23,0
	            adc     r31,r23          
	            lpm                        ;get data from program memory
	            mov     r20,r0             ;put data to rs232              
	            rcall   TX232
	            ret
	
	;***************************************
	;*    subrutine for show adc on pc     *
	;***************************************
	sendch:     ldi     r20,0x0c
	            rcall   TX232  
	            ldi     r16,64    
	            ldi     ZH,high(CH0*2)
	            ldi     ZL,low(CH0*2)  
	send1:      lpm     
	            adiw    ZL,1
	            mov     r20,r0
	            rcall   TX232
	            dec     r16
	            brne    send1    
	            ldi     r20,0x0a
	            rcall   TX232
	            ldi     r20,0x0d
	            rcall   TX232
	send3:      ldi     r18,8
	            ldi     r17,0x00           ;start  chanl 0 ADC
	send2:      out     ADMUX,r17
	            sbi     ADCSR,ADEN         ;enable ADC
	            sbi     ADCSR,ADSC         ;start conversion  
	send4:      sbis    ADCSR,ADIF
	            rjmp    send4                    
	            rcall   ATOD             
	            ldi     r20,0x20
	            rcall   TX232
	            rcall   TX232
	            rcall   TX232
	            rcall   TX232
	            rcall   TX232   
	            inc     r17  
	            dec     r18     
	            brne    send2 
	            ldi     r20,0x0d
	            rcall   TX232
	            rcall   switch 
	            rcall   p72io 
	            rjmp    send3 
	            ret    

	;****************************
	;*      PROGRAM READKEY     *
	;*                          * 
	;*   key     : portc        * 
	;*   led     : portb        * 
	;****************************
	switch:   ser     r16
	          out     DDRD,r16      ;set portb as output 
	          clr     r16
	          out     DDRC,r16      ;set portc as input
	          ser     r16 
	          out     PORTC,r16        
	switch1:  in      r26,PINC      ;read portc key from portc
	          out     PORTD,r26     ;show press key 
	          ret


	p72io:    ldi     r17,0xff      ;set port b,c,d as output
	          out     DDRB,r17         
	          out     DDRC,r17
	          out     DDRD,r17
           
	;*************************************
	;*             P72ioZ80              *
	;*************************************
	loop1:    ldi     r17,0b10101010
	          out     PORTB,r17 
	          rcall   delay  
	          rcall   delay
	          ldi     r17,0b01010101  
	          out     PORTB,r17  
	          rcall   delay  
	          rcall   delay
	          ret   

	;************************************         
	;*            delay program         *  
	;************************************   
	delay:     ldi     r22,0x04
	dly1:      ldi     r23,0x0ff
	dly2:      ldi     r24,0x0ff
	dly3:      dec     r24
	           brne    dly3  
	           dec     r23
	           brne    dly2   
	           dec     r22
	           brne    dly1
	           ret   
                  
  
    
	TX232:      sbis    USR,UDRE           ;wait until UDRE = 1
	            rjmp    TX232 
	            out     UDR,r20            ;send data to serial  
	            ret 
            
	RX232:      sbis    USR,RXC            ;wait until have data in buffer
	            rjmp    RX232
	            in      r20,UDR            ;read data from buffer
	            rcall   TX232  
	            ret 
        
	.CSEG
	HEXTBL:    .db     0x30,0X31           ;table for convert hex to ascii
	           .db     0X32,0X33
	           .db     0X34,0X35
	           .db     0X36,0X37
	           .db     0X38,0X39
	           .db     0X41,0X42
	           .db     0X43,0X44
	           .db     0X45,0X46
               

	.CSEG
	CH0:       .db     0x43,0x48,0x30,0x20,0x20,0x20,0x20,0x20
	           .db     0x43,0x48,0x31,0x20,0x20,0x20,0x20,0x20
	           .db     0x43,0x48,0x32,0x20,0x20,0x20,0x20,0x20
	           .db     0x43,0x48,0x33,0x20,0x20,0x20,0x20,0x20
	           .db     0x43,0x48,0x34,0x20,0x20,0x20,0x20,0x20
	           .db     0x43,0x48,0x35,0x20,0x20,0x20,0x20,0x20
	           .db     0x43,0x48,0x36,0x20,0x20,0x20,0x20,0x20
	           .db     0X43,0x48,0x37,0x20,0x20,0x20,0x20,0x20                                

ส่วนใครที่สนใจตัวอย่างโปรแกรมทั้งหมด สามารถ download ไฟล์สำหรับบอร์ด ET-AVR V1 ได้เลยครับ


เขียนโดย : ETT
Author : ETT team
e-mail : sales@etteam.com
วันที่ทำการปรับปรุง : ๑๖ พ.ย. ๒๕๔๓