kotsam
Joined: 17 Nov 2005 Posts: 29
|
16 May 2008 12:42 pic18f timer |
|
|
|
|
Hi All ,
i have worte code shown below, this code is comapring between to registers , RAM0 and ADC1 as shown in the code below , the problem is in this code , when i'm writting value to this registers without , using RCREG , just moving varibales to them , the compare function works fine , but when i'm trying to use RS232 to wirte into the ram location (RAM0 for example) the comapring is not working as expected , it means the RAM0 has problem of the entered value ; i.e what i did i transfered the value to the TXREG to see if i'm writting correctly , and what i find out that i get strange signals (for example if i'm writting to ram0 5 i get back (!@#$%) but on the other had if i loop the tranfer operation i get the correct value ,
can anybody please help me out i have spent more than one week trying to find out where is the problem and i could find it ;
| Code: |
#include <p18F4550.inc>
CONFIG WDT=OFF; disable watchdog timer
CONFIG MCLRE=ON ;no MCLR
CONFIG DEBUG=ON ;disable Debug Mode
CONFIG LVP = OFF; Low-Voltage programming disabled (necessary for debugging)
CONFIG FOSC = HS ; for 20MHZ
Delay1 res 1;reserve 1 byte for the variable Delay1
Delay2 res 1;reserve 1 byte for the variable Delay2
;**********************************************************
; Start of executable code
org 0x000 ; Reset vector
nop
goto Main
;**********************************************************
; Interrupt vector
org 0x008 ; interrupt vector
goto IntService
;**********************************************************
; Main program
Main
call Initial ; Initialize everything
MainLoop
goto MainLoop ; Infinite loop
;**********************************************************
; Initial Routine
Initial
CLRF RAM0
MOVL W 0x0F
MOVWF ADCON1 ;all digital I/O
MOVLW 0x07
MOVWF CMCON ;all digital I/O
CLRF LATB
CLRF TRISB ;PORTB all output 0
CLRF LATD
CLRF TRISD ;PORTB all output 0
movlw .129 ;
movwf SPBRG
bsf BAUDCON,BRG16
bcf TXSTA,SYNC
bsf TXSTA,TXEN ; Enable transmit
bcf TXSTA,BRGH ; Select high baud rate
bsf RCSTA,SPEN ; Enable Serial Port
bsf RCSTA,CREN ; Enable continuous reception
bcf PIR1,RCIF ; Clear RCIF Interrupt Flag
bsf PIE1,RCIE ; Set RCIE Interrupt Enable
bsf INTCON,PEIE ; Enable peripheral interrupts
bsf INTCON,GIE ; Enable global interrupts
;bsf TRISC, 7 ; Set RC7/RX as an input pin
movlw b'10101111'
movwf TRISC
CALL CompareFuncion
LOOP
return
;**********************************************************
; Interrupt Service Routine
; This routine is called whenever we get an interrupt.
IntService
btfsc PIR1, RCIF ; Check for a Serial Port Reception interrupt
call Receive
retfie
;**********************************************************
; Receive Routine
; This routine is called whenver we get a Serial Port Reception interrupt.
Receive:
;this indirect addressing
;process data to wanted ram location ; by writting the data and the address
movf RCREG,W ;
movwf FSR0L ;here is the selecting address , ; i know it should include FSR0H but its not required for this test
movf RCREG,W ;
movwf INDF0 ; writting into the selected address
RETURN
CompareFuncion:
CALL SAVEINTORAM ; take the Ram value here is the problem that the ram value the recvied here not as the entered value
MOVLW D'51' ;but in the other hand if i have write not to RAM0 as i did for ADC1 it works fine ; just when i receive the value through RCREG it makes problems
MOVWF ADC1 ; put d'51' into adc1
; MOVF ADC1,W;
SUBWF RAM0; ADC1-RAM0=0
BTFSC STATUS,Z
GOTO CompareFuncion ;yes repeat
GOTO CHECKNEG ;NO
CHECKNEG
CLRF ADC1
MOVF ADC1,W
SUBWF RAM0 ;ADC1-RAM0=NEGATIVE
BTFSC STATUS,N
GOTO PoTDownAN0
GOTO PotUPAN0
PoTDownAN0: ;MOVE POT down
bSf PORTB,5 ;/CS
NOP
bSf PORTB,4 ;/UD
NOP
bCf PORTB,5 ;/CS
NOP
bCf PORTB,4 ;/UD
NOP
bSf PORTB,4 ;/UD
bSf PORTB,5 ;/CS
GOTO CompareFuncion
PotUPAN0: ; MOVE POT up
bsf PORTB,5 ;/CS
NOP
bCf PORTB,4 ;/UD
NOP
bcf PORTB,5 ;/CS
NOP
bSf PORTB,4 ;/UD
NOP
bCf PORTB,4 ;/UD
bsf PORTB,5 ;/CS
GOTO CompareFuncion
SAVEINTORAM:
MOVF RAM0,W
MOVWF TXREG ;i'm reading the value to make sure the i wrote into it correclty but i get strange signal(if i write for example 5,
;i get (!@haohuege (noises)) , but i make loop; like GOTO SAVEINTORAM I get the correct value , but its infinite loop ;
RETURN
end
|
i hope to solve this problem , where is did the mistake ???!!!!!!!!!!!!!!!
|
|
blueroomelectronics
Joined: 17 Sep 2006 Posts: 1681 Helped: 99 Location: Toronto, Canada
|
16 May 2008 14:56 timer pic18f |
|
|
|
|
Well DEBUG = ON will turn ON the debug feature...
Want something simple, try Swordfish BASIC SE (free) for the 18F
| Code: |
{
*****************************************************************************
* Name : ReadySteadyGO.BAS *
* Author : William Richardson *
* Notice : Copyright (c) 2008 blueroomelectronics *
* : All Rights Reserved *
* Date : 3/3/2008 *
* Version : 0.9beta for the Junebug 18F Laboratory *
* Notes : LEDs 3,2,1 countdown, press button #1 as soon as LED 1 is lit
* : DIP Switch #4 must be on to see results using PICkit2 UART Tool *
*****************************************************************************
}
Device = 18F1320
Clock = 8 // 8MHz clock
Include "junebug.bas"
Include "convert.bas"
Include "usart.bas"
Dim Qualify, Temp As Byte
Dim Time As String ' total time in us
Interrupt ButtonPress()
T0CON.7 = 0 ' stop the timer
If Qualify = 0
Then
Write(" Too Soon! You have to wait till LED #1 is lit.",13,10)
LED(6)
Else
Temp = TMR0L ' loads TMR0H
Time = DecToStr(((TMR0H*256)+Temp)*16,6)
Write(" Your time was 0.",Time," Seconds.",13,10)
LED(4)
EndIf
INTCON.4 = 0 '
While true
Wend
End Interrupt
OSCCON = $72 // 8 MHz clock
SetBaudrate(br9600)
INTCON2.7 = 0 ' weak pullups on
INTCON.1 = 0 ' INT0IF
INTCON.4 = 1 ' INT0IE
INTCON2.6 =0 ' INTEDG0 falling edge trigger
ADCON1 = %11110101
Write("When LED 1 lights press button #1.",13,10)
Qualify = 0
T0CON = $04 ' 1/32 prescaler on timer 0, 16 bit mode
TMR0L = 0 ' clear the timer
TMR0H = 0
LED(3) ' Ready
DelayMS(1000)
INTCON.1 = 0 ' clear the interrupt flag
Enable(ButtonPress)
LED(2) ' Steady
DelayMS(500)
Qualify = 1 ' don't disqualify yourself by pressing too early
T0CON.7 = 1 ' start the timer
LED(1) ' GO
DelayMS(1000) ' 1 second timeout
INTCON.4 = 0 ' too slow disable interrupt
LED(5)
Write(" Too Slow, press RESET to try again.",13,10)
While true
Wend
End |
|
|