What I'm trying to do is create a tiny circuit that allows me to connect my PC to my special wireless network. I'm using a microcontroller because I want it to convert commands from the PC to the network format which I will define later.
I took a suggestion from somewhere that unused inputs on IC's should be tied to VCC or GND. I tied both of them to VCC and now I learned based on the datasheet that I connected an internal 5K resistor between 5V and ground via the HIN232CP. Perhaps I should just break the unused input pins off the IC to not waste 1mA power? or is that a bad idea?
All items in the schematic should be explanatory in terms of identification. On the top-left is a connection point in which I connect the HM-TRP radio module.
I began coding the microcontroller and testing things out. So far, no heat, and the LED responds correctly, however the software uart does not function correctly.
Ideally, I'd like to receive data at 38400bps but I'd be happy if someone could adjust this code to make it work even at 1200bps so I know how to fix it.
It's either that, or I have max232 wired up wrong. Also, I used 2.2uF capacitors around the max232 because someone suggested I didn't need 10uF and I borrowed a circuit from the internet.
What can I do to solve this?
Here's the code, and the circuit follows.
I took a suggestion from somewhere that unused inputs on IC's should be tied to VCC or GND. I tied both of them to VCC and now I learned based on the datasheet that I connected an internal 5K resistor between 5V and ground via the HIN232CP. Perhaps I should just break the unused input pins off the IC to not waste 1mA power? or is that a bad idea?
All items in the schematic should be explanatory in terms of identification. On the top-left is a connection point in which I connect the HM-TRP radio module.
I began coding the microcontroller and testing things out. So far, no heat, and the LED responds correctly, however the software uart does not function correctly.
Ideally, I'd like to receive data at 38400bps but I'd be happy if someone could adjust this code to make it work even at 1200bps so I know how to fix it.
It's either that, or I have max232 wired up wrong. Also, I used 2.2uF capacitors around the max232 because someone suggested I didn't need 10uF and I borrowed a circuit from the internet.
What can I do to solve this?
Here's the code, and the circuit follows.
Code:
;Attempted 9.6kbps or 19.2kbps or 38.4kbps channel half duplex
SWUBUF equ 30h ;space for UART functions
;Math:
;xtal = 22118400hz
;baud = 19200bps
;bit time=((xtal/baud)/12)/2=48
;to timer reload format: 256-48=208=D0h
BITTIM equ 0D0h
SW_INT bit 01h ;Set = Byte processed. Uart functions stall until this bit is cleared again
SW_XMIT bit 0h ;Set = Transmit Data, Clear = Receive Data
S_IN bit P3.2 ;Bit from PC via MAX232
S_OUT bit P3.3 ;Bit to PC via MAX232
SWUCTR equ SWUBUF ;State Backup variable
SWIDAT equ SWUBUF+1 ;Temporary Data
SWDAT equ SWUBUF+2 ;User Data
LED1 equ P1.7
LED2 equ P1.6
RCFG equ P1.5
org 0h
ljmp main
org 000Bh ;timer 0 interrupt run point
ljmp swruart
org 0030h ;Some spot after interrupt handler addresses
;UART ROUTINE executes every HALF bit time
swruart:
push PSW ;Save user's carry flag
mov C,S_IN ;Read bit (just in case)
xch A,SWUCTR ;Accumulator=State. (PUSH ACC uses extra cycle)
jnb ACC.4,swrdat ;Data mode = State < 16 = Jump
jb ACC.0,swendb ;State 16=Stop bit processing.
jnb SW_XMIT,nxmit2 ;Are we transmitting?
setb S_OUT ;Yes, so output stop bit (high)
clr C ;Clear carry (to avoid processing receive check)
setb SW_INT ;and set Flag. Transmit=DONE
nxmit2:
jnc swreb ;Is received character stop bit?
mov SWDAT,SWIDAT ;Yes, so update data
setb SW_INT ;and set Flag. Receive=DONE
swreb:
inc A ;Increment State
xch A,SWUCTR ;Backup State and restore Accumulator
pop PSW ;and user Carry
reti ;and exit
;STATE=17 = Wait for new data
swendb:
jnb SW_XMIT,nxmit ;Are we transmitting?
jb SW_INT,nxmit ;Yes. Is complete flag set?
clr S_OUT ;No, Send out start bit
mov SWIDAT,SWDAT ;and load data
clr A ;and set state to 0 (data processing mode)
setb C ;and set carry (to avoid receive check)
nxmit:
jc norst ;Is received character start bit?
jb SW_XMIT,norst ;Yes, Are we transmitting?
clr A ;No. set state to 0 (data processing mode)
norst:
xch A,SWUCTR ;Backup state and restore accumulator
pop PSW ;and user carry
reti ;and exit
;State 0-15 - Data processing
swrdat:
jnb ACC.0,swskn ;Do we have the right half time?
xch A,SWIDAT ;Yes, so switch accumulator with temporary data
RRC A ;shift in incoming bit and shift out possible output bit
xch A,SWIDAT ;switch data and accumulator back
jnb SW_XMIT,swskn ;Are we in transmitting mode?
mov S_OUT,C ;Yes, so output correct character
swskn:
inc A ;Increment State
xch A,SWUCTR ;Backup state and restore accumulator
pop PSW ;and user carry
reti ;and exit
uartcfg:
mov TH0,#BITTIM ;Set timer to run every 1/2 bit time
anl TMOD,#0F2h
orl TMOD,#02h ;Set timer 0 to auto-reload mode and internal
mov SWUCTR,#0FFh ;Set state to all bits set so it waits for data
clr SW_INT ;clear interrupt
setb SW_XMIT ;start in transmit mode
setb TR0 ;start timer 0
setb EA ;Enable all interrupts
setb ET0 ;enable timer 0 interrupt
ret
main:
mov P1,#0FFh ;restore ports
mov P3,#0FFh
clr LED1 ;turn on a light
clr RCFG ;no radio config
lcall uartcfg ;setup UART
mov DPTR,#test ;Set pointer to test message
redo:
clr A
movc A,@A+DPTR
inc DPTR ;Load next character from rom
jz endt1 ;end transmission if character is null
;wait until previous transmission is done
siw:
nop
jnb SW_INT,siw
mov SWDAT,A ;setup new byte to transmit
setb SW_XMIT ;Let UART know we want to transmit
clr SW_INT ;Clear interrupt (starts transmission)
sjmp redo ;Go back and do next character
endt1:
;Change LED color
cpl LED1
cpl LED2
endt:
clr SW_XMIT ;Let UART know we want to receive
clr SW_INT ;Clear interrupt (starts reception)
;wait until byte is received
sirw:
nop
jnb SW_INT,sirw
inc SWDAT ;add 1h to byte
setb SW_XMIT ;Let UART know we want to transmit
clr SW_INT ;Clear interrupt (starts transmission)
;wait until byte is sent
sirw2:
nop
jnb SW_INT,sirw2
sjmp endt ;go back and wait for next character
;Test data
test:
db '*************************************',0Dh,0Ah
db '* This is a test! OOOOOOOOOO *',0Dh,0Ah
db '* This is a test! O O *',0Dh,0Ah
db '* This is a test! O O *',0Dh,0Ah
db '* This is a test! O O *',0Dh,0Ah
db '* This is a test! OOOOOOOOOO *',0Dh,0Ah
db '* This is a test! K KK *',0Dh,0Ah
db '* This is a test! K KK *',0Dh,0Ah
db '* This is a test! KKK *',0Dh,0Ah
db '* This is a test! K KK *',0Dh,0Ah
db '* This is a test! K KK *',0Dh,0Ah
db '*************************************',0Dh,0Ah,0Dh,0Ah
db 00h,00h ;00h = end of data