wifi robot/car with PIC16F690

Discussion in 'Programmer's Corner' started by hysteria92, Feb 26, 2010.

  1. hysteria92

    Thread Starter New Member

    Feb 26, 2010
    5
    0
    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:
    Code ( (Unknown Language)):
    1.  
    2. #define DEBUG 0
    3. greenLED    equ b'00010000'
    4. redLED         equ b'01000000'
    5.  
    6.  
    7. right         equ b'00001000'
    8. left         equ b'00000100'
    9. back         equ b'00000010'
    10. forward     equ b'00000001'
    11.  
    12.  
    13. LIST P=16F690, R=DEC ; Use the pic16f690 and decimal system
    14.         #include
    15.             __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
    16.  
    17. CBLOCK 0x20 ; Declare variable addresses starting at 0x20
    18. dataL
    19.         in_char
    20.      temp
    21.     d1
    22.         d2    
    23. ENDC
    24.  
    25. ORG 0x000 ; Program starts at 0x000
    26.  
    27. ;
    28. ; --------------------------------
    29. ; set ANALOG/DIGITAL INPUTS PORT C
    30. ; --------------------------------
    31. ;
    32. cm1con0=0 ; Comparator 1 disabled
    33. cm2con0=0 ; Comparator 2 disabled
    34. ansel=0 ; Pins to be Digital
    35. anselh=0 ; Pins to be Digital
    36. ;
    37. ; ----------------
    38. ; INITIALIZE PORTS
    39. ; ----------------
    40. ;
    41. movlw b'00000000' ; set up portC
    42. movwf PORTC
    43.  
    44. movlw b'10000000' ; RB7(TX)=1 others are 0
    45. movwf PORTB
    46.  
    47. bsf STATUS,RP0 ; RAM PAGE 1
    48.  
    49. movlw 0x00
    50. movwf TRISC ; portC all pins output
    51.  
    52. movlw b'00100000' ; RB5(RX)=input, others output
    53. movwf TRISB
    54.  
    55. ; ------------------------------------
    56. ; set BAUD RATE TO COMMUNICATE WITH PC
    57. ; ------------------------------------
    58. ; Boot Baud Rate = 9600, No Parity, 1 Stop Bit
    59. ;
    60. movlw 0x19 ; 0x19=9600 bps (0x0C=19200 bps)
    61. movwf SPBRG
    62. movlw b'00100100' ; brgh = high (2)
    63. movwf TXSTA ; enable Async Transmission, set brgh
    64.  
    65. bcf STATUS,RP0 ; RAM PAGE 0
    66.  
    67. movlw b'10010000' ; enable Async Reception
    68. movwf RCSTA
    69. ;
    70. ; ------------------------------------
    71. ; PROVIDE A setTLING TIME FOR START UP
    72. ; ------------------------------------
    73. ;
    74. clrf dataL
    75. settle: decfsz dataL,F
    76. goto settle
    77.  
    78. movf RCREG,W
    79. movf RCREG,W
    80. movf RCREG,W ; flush receive buffer
    81.  
    82. ;
    83. ; ---------
    84. ; MAIN LOOP
    85. ; ---------
    86. ;
    87.      clrf PORTC
    88. movlw redLED
    89.      movwf PORTB
    90.  
    91. call message ; prints out a message if DEBUG is 1
    92. call check_init
    93.      bsf PORTB,7
    94. loop: call receive_delay ; wait for a char
    95. call add_green     ;set greenLED on
    96.      call process_input
    97. call send ; send the char
    98. goto loop
    99.  
    100. process_input:
    101. ; Make sure that Forward & Backward cannot be ON at the same time
    102. ; Make sure that Left & Right cannot be ON at the same time
    103. ; Output result to PORTB
    104.  
    105.      MOVWF temp
    106. BTFSC temp,0         ;//Execute next line if temp,0 is HIGH
    107.      BCF temp,1
    108. BTFSC temp,2         ;//Execute next line if temp,2 is HIGH
    109.      BCF temp,3
    110. BTFSC temp,6         ;//Execute next line if temp,6 is HIGH
    111. BCF temp,7
    112.      MOVF temp,0
    113. andlw b'00001111'
    114. movwf PORTA
    115. movf temp,0
    116. andlw b'11110000'
    117. movwf PORTB
    118. movf temp,0
    119.      return
    120.  
    121. add_green:
    122. movwf temp
    123. btfss temp,7         ;If greenLED is LOW execute next line
    124. bsf temp,7
    125.      movf temp,0
    126.      return
    127.  
    128. ;
    129. ; -------------------------------------------
    130. ; RECEIVE CHARACTER FROM RS232 AND STORE IN W
    131. ; -------------------------------------------
    132. ; This routine does not return until a character is received.
    133. ;
    134. receive:
    135. btfss PIR1,RCIF ; (5) check for received data
    136. goto receive
    137.  
    138. data_received:
    139. movf RCREG,W ; save received data in W
    140. return
    141.  
    142.  
    143. check_init:
    144. ; 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
    145.  
    146. call receive_delay
    147. sublw 'j'
    148.      btfss STATUS, Z
    149.      goto check_init
    150.      call receive_delay
    151.      sublw 'b'
    152.      btfss STATUS, Z
    153.      goto check_init
    154.      call receive_delay
    155.      sublw 'p'
    156.      btfss STATUS, Z
    157.      goto check_init
    158.      call receive_delay
    159.      sublw 'r'
    160.      btfss STATUS, Z
    161.      goto check_init
    162.      call receive_delay
    163.      sublw 'o'
    164.      btfss STATUS, Z
    165.      goto check_init
    166.  
    167. #if DEBUG
    168. ;prints message and goes in to an endless loop of flashing greenLED
    169.     call message
    170.  
    171. Loop:    
    172.      movlw greenLED
    173.      movlw 0xff
    174.      movwf PORTA        ;set all bits on
    175. call Delay        ;this waits for a while!
    176. movlw 0x00
    177.      movwf PORTA
    178. call Delay
    179.      goto Loop        ;go back and do it again
    180. #endif
    181.  
    182. return
    183.  
    184.  
    185. receive_delay:
    186.                 ;299993 cycles
    187.      movlw    0x5E
    188.      movwf    d1
    189.      movlw    0xEB
    190. movwf    d2
    191. delay_0:
    192. btfsc PIR1,RCIF      ;check for serial data
    193. goto got_data         ;if we do, goto got_data!
    194. decfsz    d1, f
    195. goto    $+2
    196.      decfsz    d2, f
    197. goto    delay_0
    198. movlw 0x00
    199. return
    200.  
    201. got_data:
    202. ;move data in to W
    203.  
    204.     btfsc PIR1,RCIF
    205.     movf RCREG,W
    206.     return
    207.  
    208. #if DEBUG
    209. Delay:    
    210.     movlw    d'250'        ;delay 250 ms (4 MHz clock)
    211.     movwf    temp
    212. d11:    
    213.     movlw    0xC7
    214.     movwf    d1
    215.     movlw    0x01
    216.     movwf    d2
    217. Delay_0:
    218.     decfsz    d1, f
    219.     goto    $+2
    220.     decfsz    d2, f
    221.     goto    Delay_0
    222.  
    223.     decfsz    temp, f
    224.     goto    d11
    225.     retlw    0x00
    226. #endif
    227.  
    228. ;
    229. ; -------------------------------------------------------------
    230. ; SEND CHARACTER IN W VIA RS232 AND WAIT UNTIL FINISHED SENDING
    231. ; -------------------------------------------------------------
    232. ;
    233. send:
    234. #if DEBUG
    235.     movwf TXREG ; send data in W
    236.  
    237. TransWt:
    238.      bsf STATUS,RP0 ; RAM PAGE 1
    239. WtHere: btfss TXSTA,TRMT ; (1) transmission is complete if hi
    240. goto WtHere
    241.     bcf STATUS,RP0 ; RAM PAGE 0
    242. #endif
    243. return
    244. ;
    245. ; -------
    246. ; MESSAGE
    247. ; -------
    248. ;
    249. message
    250. #if DEBUG
    251.      movlw '1'
    252. call send
    253. movlw '6'
    254. call send
    255. movlw 'F'
    256. call send
    257. movlw '6'
    258. call send
    259. movlw '2'
    260. call send
    261. movlw '8'
    262. call send
    263. movlw ' '
    264. call send
    265. movlw 'a'
    266. call send
    267. movlw 'l'
    268. call send
    269. movlw 'i'
    270. call send
    271. movlw 'v'
    272. call send
    273. movlw 'e'
    274. call send
    275. movlw 0x0D        ; CR
    276. call send
    277. movlw 0x0A        ; LF
    278. call send
    279. #endif
    280. return
    281.  
    282. .END
    283.  
    Any help would be greatly appreciated...
     
    Last edited: Feb 26, 2010
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I do not know if this help. From your code, it seams like you are using internal RC osc in defualt mode (4 Mhz) If you look at table 12-5 in the data sheet (16f690), you will se that 9600 as baud rate is not selectable.
    Also take a look here http://forum.allaboutcircuits.com/misc.php?do=bbcode#code It will make your posting with code more easy to read.
     
    Last edited: Feb 26, 2010
  3. hysteria92

    Thread Starter New Member

    Feb 26, 2010
    5
    0
    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 :)
     
  4. hysteria92

    Thread Starter New Member

    Feb 26, 2010
    5
    0
    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
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
Loading...