要顯示 ADC 的結果到 PC 的螢幕, 不是新問題, 也不是難題, 不過就是要特別給這個 ATTINY26 量身訂造一個. 不是最好的寫法, 不過最直接.


接好線, DEMO 會在 PC 螢幕顯示一個字母 A
 
實際上, 只要計算好 delay_bit_rate, baud rate 可以做到 115200, crystal 可以是任何速度, 只要保證 bit rate 的計算符合下面註解的方式, 早在 68705 的年代, 這個方法一直沿用到今天, 不管哪一家的MCU / CPU, 這個都是最簡單的方法, 除非有硬件 UART.
 
例如 : 
Tb (Bit time) = 1 / baud rate * 10^6 (unit : us) = 1/9600 * 10^6 = 104.16 us 
Tc (CPU cycle time) = 1 / crystal frequency (unit : us) = 1 / 12MHz = 0.083 us
Tt (Cycle time for Tx operation) = 12 CPU cycles of this case = 12 x 1 / 12MHz = 1 us
 
hence, 
delay_bit_rate = (Tb - Tt) / Tc
 
usually, no more than 3% of bit timing drift, byte will be received correctly because RS232 receiver will sync every start bit at its' falling edge in logically (raising edge in electrically)
 
 
 

;*------------------------------Mnamonic code below
; software RS232 transmission, 依照 RS232 的格式發送一個BYTE
; I/O direct drives the PC com port, no need MAX232
; wayen_cwwong@yahoo.com, 2008-Sep-03
; 12MHz crystal, every instruction cycle is 1/1.2us
; RS232 bit time (depends) = 1 / baud rate, ie. 1 / 9600 = 104,16us per bit
; RS232 format, N81, LSB first
; input : 2 bytes will be sent over RS232, r17:r16 (this demo, send one byte only)
; output : N/A
; uses : r0, scratch register


; RS232 frame for a byte of data & voltage level
;  ____                           _____  idle or logic 1 of RS232, 0 to -12V, I/O pin = 0
;           |_XXXXXXXX              logic 0 of RS232, +3V to + 12V, I/O pin = 1
;
; the I/O direct drive technique, it is not 100% comply 
; to EIA232, but it works for this our purpose.


.equ Tx =1 ; 0 to 7, any pin of PortA is ok to used as Tx line of RS232
.def bit_counter = r17 ;
.def data_8bit = r0 ;


Tx_8bit_data:
; ldi r17, 0b10000001 ; test pattern of 8 bit data = 1000,0001 for debug purpose
ldi r17, "A" ; test pattern of 8 bit data = letter A
mov data_8bit, r17 ; save the test pattern to defined register

ldi bit_counter,10 ; 1 start bit + 8bit char + 1 stop bit = 10
; setup the counter, total 10bit need to sent

clc ; (C)( 76543210 ) ; C bit and bit name of data_8bit
   ; (0)( ???????? ) ;current status, C=0, data_8bit is ready to sent

send_0_or_1: ; decide to send 0 or 1
brcs send_logic_1 ; **[2] if C=1, jump, [2] means 2 machine cycles
; [1] if C=0, no jump, [1] means 2 machine cycles 
nop ; [1] compensate 1 cycle if no jump above

send_logic_0: ; start bit is always logic_0, reach here
sbi porta,Tx ; **[2] I/O pin = +5v (high), Tx = logic 0 of RS232 signal, C bit no change
rjmp delay_bit_rate ; **[2] delay until time for 1bit is over. C bit no change

send_logic_1:
cbi porta,Tx ; [2]I/O pin = 0v (low), Tx = logic 1 of RS232 signal, C bit no change
rjmp delay_bit_rate ; [2] delay until time for 1bit is over. C bit no change

delay_bit_rate: ; delay_cycles = 104.16 x 1.2 - 12 cycle  =113 (9600 baud & 12MHz crystal)
nop ; software delay loop, detial is TBA
.......

dec bit_counter ; **[1] one bit sent, decrease the counter
breq complete ; **[1] if counter=0, 10 bits sent, jump to complete

sec ; **[1] no jump above, reached here, not finish yet
ror data_8bit ; **[1] (C)[ C7654321 ] ;next data bit shift to C
   ;       (1)[ 1xxxxxxx ] ; when finish, data_8bit changed to 0xff
rjmp send_0_or_1 ; **[2] go back to the loop, send next bit

complete:
ret ; no more bit need to send, complete
arrow
arrow
    全站熱搜

    xiaolabaDIY 發表在 痞客邦 留言(0) 人氣()