Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[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
 
  • Like
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.
 
  • Like
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:

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.


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

10.1 PORTA, TRISA and LATA Registers

...
...
...

Several PORTA pins are multiplexed with analog inputs,
the analog VREF+ and VREF- inputs and the comparator
voltage reference output. The operation of pins RA5
and RA3:RA0 as A/D converter inputs is selected by
clearing/setting the control bits in the ADCON1 register
(A/D Control Register 1).

Note: On a Power-on Reset, RA5 and RA3:RA0
are configured as analog inputs and read
as ‘0’. RA4 is configured as a digital input.

All other PORTA pins have TTL input levels and full
CMOS output drivers.

The TRISA register controls the direction of the RA
pins, even when they are being used as analog inputs.
The user must ensure the bits in the TRISA register are
maintained set when using them as analog inputs.

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

Note: On a Power-on Reset, RB4:RB0 are
configured as analog inputs by default and
read as ‘0’; RB7:RB5 are configured as
digital inputs.

By programming the Configuration bit,
PBADEN (CONFIG3H<1>), RB4:RB0 will
alternatively be configured as digital inputs
on POR.


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

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top