[SOLVED] PIC18FX & HiTech C. Simple code not working, please help.

Status
Not open for further replies.

rgc

Junior Member level 1
Joined
May 3, 2011
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,430
I'm an absolute begginer to Hi-Tech C compilers, and I can't get the following code to work. It should be ridiculously simple, if I turn RB0 high A0-7 will turn high, and vice versa, but somehow I managed to get it wrong. It doesnt matter RB0's state, nothing changes on LATA which will always be low.

Some info:
- I'm using HTC version 9.80 for PIC18
- I am using a crystal resonator of 20 MHz
- My circuit is correct.

Code:
#include <htc.h>

#pragma config FOSC = HS
#pragma config PWRT = OFF
#pragma config BOR = OFF

int main(void){
	TRISA = 0x00;
	TRISB  = 0xFF;

	while(1){
		LATA=RB0?0xFF:0x00;
	}
}

What am I doing wrong?
 

What model of the PIC18F family are you using in your design?

There are often several peripheral modules which share various PORTs, especially PORTA, you may need to disable these peripheral modules before you can use PORTA as digital I/O.

A few more issues:

I would strong recommend you disable the Watchdog Timer in the Configuration Register Settings.

Fully configure your oscillator/PLL within your Configuration Register Settings, so that the actual system clock frequency is know.


BigDog
 
Reactions: rgc

    rgc

    Points: 2
    Helpful Answer Positive Rating
PORTA normally can function as analog input. So, you need set address (ADCON0) to set as digital I/O.
 
Reactions: rgc

    rgc

    Points: 2
    Helpful Answer Positive Rating
What model of the PIC18F family are you using in your design?

There are often several peripheral modules which share various PORTs, especially PORTA, you may need to disable these peripheral modules before you can use PORTA as digital I/O.

I am using PIC18F4550, and my problem is not with PORTA, its with digital input on PORTB (RB0), but nevertheless it does makes sense, maybe there is another peripheral that I sould disable on RB0 before using it as digital I/O? The datasheet says: RB0/AN12/INT0/FLT0/SDI/SDA on pin 33.

Edit: By the way, that same code works like a charm on Microchip's C18 compiler. Also, I'm getting this warning by the compiler: Warning [1385] xxx.c; 13.19 variable "RB0" is deprecated.

Fully configure your oscillator/PLL within your Configuration Register Settings, so that the actual system clock frequency is know.

Thanks, do you have any reference material teaching how to do that?
 
Last edited:



Reference: PIC18F2455/2550/4455/4550 Datasheet, Section: 10.1 PORTA, TRISA and LATA Registers, Pg. 113


EXAMPLE 10-1: INITIALIZING PORTA
Code:
CLRF PORTA ; Initialize PORTA by
; clearing output
; data latches
CLRF LATA ; Alternate method
; to clear output
; data latches
MOVLW 0Fh ; Configure A/D
MOVWF ADCON1 ; for digital inputs
MOVLW 07h ; Configure comparators
MOVWF CMCON ; for digital input
MOVLW 0CFh ; Value used to
; initialize data
; direction
MOVWF TRISA ; Set RA<3:0> as inputs
; RA<5:4> as outputs


Example in C Code:
Code:
LATA = 0x00;
ADCON1 = 0x0F;
CMCON = 0x07;
TRISA = 0x00;

Reference: PIC18F2455/2550/4455/4550 Datasheet, Section: 10.2 PORTB, TRISB and LATB Registers, Pg. 116



EXAMPLE 10-2: INITIALIZING PORTB
Code:
CLRF PORTB                 ; Initialize PORTB by
                                 ; clearing output
                                 ; data latches
CLRF LATB                   ; Alternate method
                                 ; to clear output
                                 ; data latches
MOVLW 0Eh                 ; Set RB<4:0> as
MOVWF ADCON1           ; digital I/O pins
                                 ; (required if config bit
                                 ; PBADEN is set)
MOVLW 0CFh               ; Value used to
                                 ; initialize data
                                 ; direction
MOVWF TRISB              ; Set RB<3:0> as inputs
                                 ; RB<5:4> as outputs
                                 ; RB<7:6> as inputs

Example in C Code:
Code:
LATB = 0x00;
ADCON1 = 0x0E;
TRISB = 0xFF:


BigDog
 

Works like a charm! Thank you very much!
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…