skubaig
Newbie level 6
Code:
;***************PortB Variable definitions
SWPORTB EQU B'00101111' ; Switches are connected to PORTB
;SWTRIS EQU TRISB
SW1 EQU 0
SW2 EQU 1
SW3 EQU 2
SW4 EQU 3
PICOUT EQU 4
PICTXIn EQU 5
;End of Definitions
UDATA
Delay_i res 1 ; Delay Loop Count
;***************************************************
;********BAUDRATE SETTINGS**************************
#define BD0 0x00 ;BD0=921600 time delay =0.01041ms
#define BD1 0x01 ;BD1=460800 time delay =0.02082ms
#define BD2 0x02 ;BD2=230400 time delay =0.04166ms
#define BD3 0x03 ;BD3=115200 time delay = 0.0833ms
#define BD4 0x04 ;BD4=57600 time delay =0.1666ms
#define BD5 0x05 ;BD5=38400 time delay =0.25ms
#define BD6 0x06 ;BD6=19200 time delay = 0.5ms
#define BD7 0x07 ;BD7=9600 time delay = 1ms
#define BD8 0x08 ;BD8=4800 time delay = 2ms
#define BD9 0x09 ;BD9=2400 time delay = 4ms
#define BD10 0x0a ;BD10=1200 time delay = 8ms
#define BD11 0x0b ;BD11= 300 time delay = 32ms
#define BD12 0x0c ;BD12 = 110 time delay = 87ms
;****************************************************
;LOOKUP TABLE***************************************
;***************** Main Program
org 0x0000
;Options
bsf STATUS,RP0 ;Select Bank1
movlw B'00000000'
;bit7-Set to disable weak pull-ups PORTB
;bit6-falling edge interupt
;bit5-TMR0 clock source internal cycle clock
;bit4-NA
;bit3-Prescaler assigned to TMR0
;bit<2:0> TMR0 prescaler 1:128
movwf OPTION_REG
;interrupts
movlw B'00000000' ; 0 -bit7-Global interupts disabled - enable later
; 0 -bit6-Peripheral interupts disabled - enable later
; 0 -bit5-TMR0 overflow disabled
; 0 -bit4-INTE disabled external interupts RB0
; 0 -bit3-RB port change interupt disabled
; 0 -bit2-TMR0 overflow flag bit cleared
; 0 -bit1-INTF external interupt flag
; 0 -bit0-RB port change flag bit cleared
movwf INTCON
movlw b'00101111' ;Set PortB 0,1,2,3,5 as input and pin4 as output
movwf TRISB
bcf STATUS,RP0 ;Select Bank0
;
MainLoop
bsf STATUS, RP0
movlw b'00101111' ;Set PortB 0,1,2,3,5 as input and pin4 as output
movwf TRISB
bcf STATUS,RP0 ;Select Bank0
movf SWPORTB, W ; read PORTB
swapf SWPORTB, W
andlw 0x0F ; Consider only the lower 4-bits RB0-RB3
addwf PCL,1
goto BAUDRATE_0
goto BAUDRATE_1
goto BAUDRATE_2
goto BAUDRATE_3
goto MainLoop
BAUDRATE_0
;Here0
H1 btfss PORTB, PICTXIn ; Test for Tx pin Low
goto H1 ; Goto H1 loop
bsf PORTB, PICOUT ; Set pin4 of portb high
call Delay0.01041ms ; call 1ms delay
H2 btfsc PORTB, PICTXIn ; Test for Tx pin High
goto H2
bcf PORTB, PICOUT ; Set pin4 of portb low
call Delay0.01041ms
; goto Here0
BAUDRATE_1
;Here1
H3 btfss PORTB, PICTXIn ; Test for Tx pin Low
goto H3 ; Goto H1 loop
bsf PORTB, PICOUT ; Set pin4 of portb high
call Delay0.02082ms ; call 1ms delay
H4 btfsc PORTB, PICTXIn ; Test for Tx pin High
goto H4
bcf PORTB, PICOUT ; Set pin4 of portb low
call Delay0.02082ms
; goto Here1
;
BAUDRATE_2
;Here2
H5 btfss PORTB, PICTXIn ; Test for Tx pin Low
goto H5 ; Goto H1 loop
bsf PORTB, PICOUT ; Set pin4 of portb high
call Delay0.04166ms ; call 1ms delay
H6 btfsc PORTB, PICTXIn ; Test for Tx pin High
goto H6
bcf PORTB, PICOUT ; Set pin4 of portb low
call Delay0.04166ms
; goto Here2
BAUDRATE_3
;Here3
H7 btfss PORTB, PICTXIn ; Test for Tx pin Low
goto H7 ; Goto H1 loop
bsf PORTB, PICOUT ; Set pin4 of portb high
call Delay0.08333ms ; call 1ms delay
H8 btfsc PORTB, PICTXIn ; Test for Tx pin High
goto H8
bcf PORTB, PICOUT ; Set pin4 of portb low
call Delay0.08333ms
; goto Here3
In the above code 4 DIP switches are connected to the 4pin of portB from 0-3 pins......depending on 4-bits and pin 5 of PORTB is connected to TX line of usb to 232 converter (FDTI MM232R) the pic monitors the 4DIP switches and the pin 5 depending on these conditions a output pin 4 of PORTB should generate a pulse.........pls any suggestion on the above code is appreciated .........
Last edited by a moderator: