wifi robot/car with PIC16F690

Thread Starter

hysteria92

Joined Feb 26, 2010
5
hi,
im using the PIC16F690 and PICkit 2 programmer, my aim is to make a wifi controlled car by using serial communication with a router, which the PIC will then interpret and use to control the car. I have got a program for a PIC16F628A from the internet (used for the same project), and have been trying to modify it for the PIC16F690. Just wondered if anyone would take a look at the program for me and tell me if theres anywhere ive gone wrong? Since i don't seem to be able to get it to work.

here's the code i have at the moment:
Rich (BB code):
#define DEBUG 0
greenLED    equ b'00010000' 
redLED         equ b'01000000' 
 
 
right         equ b'00001000' 
left         equ b'00000100' 
back         equ b'00000010' 
forward     equ b'00000001' 
 
 
LIST P=16F690, R=DEC ; Use the pic16f690 and decimal system
        #include 
            __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
 
CBLOCK 0x20 ; Declare variable addresses starting at 0x20
dataL
        in_char
     temp
    d1
        d2    
ENDC
 
ORG 0x000 ; Program starts at 0x000
 
;
; --------------------------------
; set ANALOG/DIGITAL INPUTS PORT C
; --------------------------------
;
cm1con0=0 ; Comparator 1 disabled
cm2con0=0 ; Comparator 2 disabled
ansel=0 ; Pins to be Digital
anselh=0 ; Pins to be Digital
;
; ----------------
; INITIALIZE PORTS
; ----------------
;
movlw b'00000000' ; set up portC
movwf PORTC
 
movlw b'10000000' ; RB7(TX)=1 others are 0
movwf PORTB
 
bsf STATUS,RP0 ; RAM PAGE 1
 
movlw 0x00
movwf TRISC ; portC all pins output
 
movlw b'00100000' ; RB5(RX)=input, others output
movwf TRISB
 
; ------------------------------------
; set BAUD RATE TO COMMUNICATE WITH PC
; ------------------------------------
; Boot Baud Rate = 9600, No Parity, 1 Stop Bit
;
movlw 0x19 ; 0x19=9600 bps (0x0C=19200 bps)
movwf SPBRG
movlw b'00100100' ; brgh = high (2)
movwf TXSTA ; enable Async Transmission, set brgh
 
bcf STATUS,RP0 ; RAM PAGE 0
 
movlw b'10010000' ; enable Async Reception
movwf RCSTA
;
; ------------------------------------
; PROVIDE A setTLING TIME FOR START UP
; ------------------------------------
;
clrf dataL
settle: decfsz dataL,F
goto settle
 
movf RCREG,W
movf RCREG,W
movf RCREG,W ; flush receive buffer
 
;
; ---------
; MAIN LOOP
; ---------
;
     clrf PORTC
movlw redLED
     movwf PORTB
 
call message ; prints out a message if DEBUG is 1
call check_init 
     bsf PORTB,7
loop: call receive_delay ; wait for a char 
call add_green     ;set greenLED on
     call process_input 
call send ; send the char
goto loop
 
process_input:
; Make sure that Forward & Backward cannot be ON at the same time
; Make sure that Left & Right cannot be ON at the same time
; Output result to PORTB
 
     MOVWF temp
BTFSC temp,0         ;//Execute next line if temp,0 is HIGH
     BCF temp,1
BTFSC temp,2         ;//Execute next line if temp,2 is HIGH
     BCF temp,3
BTFSC temp,6         ;//Execute next line if temp,6 is HIGH
BCF temp,7
     MOVF temp,0
andlw b'00001111'
movwf PORTA
movf temp,0
andlw b'11110000'
movwf PORTB
movf temp,0
     return
 
add_green:
movwf temp
btfss temp,7         ;If greenLED is LOW execute next line
bsf temp,7
     movf temp,0
     return
 
;
; -------------------------------------------
; RECEIVE CHARACTER FROM RS232 AND STORE IN W
; -------------------------------------------
; This routine does not return until a character is received.
;
receive:
btfss PIR1,RCIF ; (5) check for received data
goto receive
 
data_received:
movf RCREG,W ; save received data in W
return
 
 
check_init:
; waits to receive 'jbpro' from the router meaning that the software is up and ;running after the message is received, the greenLED is turned on and ;microcontroller can drive the car
 
call receive_delay
sublw 'j'
     btfss STATUS, Z
     goto check_init
     call receive_delay
     sublw 'b'
     btfss STATUS, Z
     goto check_init
     call receive_delay
     sublw 'p'
     btfss STATUS, Z
     goto check_init
     call receive_delay
     sublw 'r'
     btfss STATUS, Z
     goto check_init
     call receive_delay
     sublw 'o'
     btfss STATUS, Z
     goto check_init
 
#if DEBUG
;prints message and goes in to an endless loop of flashing greenLED
    call message
 
Loop:    
     movlw greenLED
     movlw 0xff
     movwf PORTA        ;set all bits on
call Delay        ;this waits for a while!
movlw 0x00
     movwf PORTA
call Delay
     goto Loop        ;go back and do it again
#endif
 
return
 
 
receive_delay:
                ;299993 cycles
     movlw    0x5E
     movwf    d1
     movlw    0xEB
movwf    d2
delay_0:
btfsc PIR1,RCIF      ;check for serial data
goto got_data         ;if we do, goto got_data!
decfsz    d1, f
goto    $+2
     decfsz    d2, f
goto    delay_0
movlw 0x00
return
 
got_data: 
;move data in to W
 
    btfsc PIR1,RCIF
    movf RCREG,W
    return
 
#if DEBUG
Delay:    
    movlw    d'250'        ;delay 250 ms (4 MHz clock)
    movwf    temp
d11:    
    movlw    0xC7
    movwf    d1
    movlw    0x01
    movwf    d2
Delay_0:
    decfsz    d1, f
    goto    $+2
    decfsz    d2, f
    goto    Delay_0
 
    decfsz    temp, f
    goto    d11
    retlw    0x00
#endif
 
;
; -------------------------------------------------------------
; SEND CHARACTER IN W VIA RS232 AND WAIT UNTIL FINISHED SENDING
; -------------------------------------------------------------
;
send: 
#if DEBUG
    movwf TXREG ; send data in W
 
TransWt:
     bsf STATUS,RP0 ; RAM PAGE 1
WtHere: btfss TXSTA,TRMT ; (1) transmission is complete if hi
goto WtHere
    bcf STATUS,RP0 ; RAM PAGE 0
#endif
return
;
; -------
; MESSAGE
; -------
;
message 
#if DEBUG
     movlw '1'
call send
movlw '6'
call send
movlw 'F'
call send
movlw '6'
call send
movlw '2'
call send
movlw '8'
call send
movlw ' '
call send
movlw 'a'
call send
movlw 'l'
call send
movlw 'i'
call send
movlw 'v'
call send
movlw 'e'
call send
movlw 0x0D        ; CR
call send
movlw 0x0A        ; LF
call send
#endif
return
 
.END
Any help would be greatly appreciated...
 
Last edited:

Thread Starter

hysteria92

Joined Feb 26, 2010
5
thanks for your quick reply, i will take a look at the data sheet and if needs be change the baud rate on the router to a selectable option from the datasheet.

will post again once ive tried this out and let you know the results :)
 

Thread Starter

hysteria92

Joined Feb 26, 2010
5
Run into a little problem... since the code i have is originally for a PIC16F628A the set up commands for the baud rates are different. Just wondered if anybody would be able to give an example code of how to set up the baud rate on a PIC16F690 at 9600 baud (according to data sheet this IS possible with 4MHz clock speed, with following settings: sync=0,brgh=1,brg16=0)

once again any help would be really appreciated
 

t06afre

Joined May 11, 2009
5,934
Sorry you are correct. The 16f690 can use 9600 as baud rate. Also a tip have you tested the MPLAB software simulator. This is a valuable tool for checking how your code behave. Google mplab simulator and you will find both documentation and videos on how to use this option
 
Top