Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

porta 0&1 not reading on 18f1230

Status
Not open for further replies.

Dr_Doggy

Well-Known Member
all defined leds are just for that, LED indicators this is my first time defining anything, how was that, is there better way to simplify? I would have liked to do this: #define LEDx PORTxbits.Rxx; (IN main): LEDx = 1; but mplab didnt like that.

anyway im more concerned with the if statement down there, as you can see there are 4 ports tied to buttons that hit a breakpoint when cnt++; however on my tests ports b0,a2,a3 are working ok, and like the other ports a1 is also on a button that goes to 0 when pressed. Also i have been testing port a0 in the if statement which is actually on a pot but is also never going to logic1 just like port a1. I disabled adc.h to ensure that it was not a conflict there, but still no results... and i have tested on leg of a1 with near 5v and have replaced microchip.... im sure this is rookie mistake....


Code:
#pragma config OSC=INTIO2, WDT=OFF
///#include <adc.h>
#include <p18f1230.h>
#include <string.h>
#include <delays.h>


#define LED1o LATAbits.LATA4 = 1;#define LED2o LATBbits.LATB3 = 1;
#define LED3o LATBbits.LATB2 = 1;#define LED4o LATAbits.LATA7 = 1;
#define LED5o LATAbits.LATA6 = 1;#define LED6o LATBbits.LATB7 = 1;
#define LED7o LATBbits.LATB6 = 1;#define LED8o LATBbits.LATB5 = 1;

#define LED1f LATAbits.LATA4 = 0;#define LED2f LATBbits.LATB3 = 0;
#define LED3f LATBbits.LATB2 = 0;#define LED4f LATAbits.LATA7 = 0;
#define LED5f LATAbits.LATA6 = 0;#define LED6f LATBbits.LATB7 = 0;
#define LED7f LATBbits.LATB6 = 0;#define LED8f LATBbits.LATB5 = 0;

//unsigned int READPOT ();

void main(){

     unsigned char cnt;
     unsigned char cnt4;
     unsigned int xxx;
   unsigned int xxx2;

     OSCCON= 0b01110000;  //8MHz please
   OSCTUNE=0b00011100;
   CMCON=0x00;


TRISA = 0b00101111;
TRISB = 0b00000001;
cnt = 0;

while(1) {

if ((PORTBbits.RB0 == 0)  ||  (PORTAbits.RA1 == 0)  ||   (PORTAbits.RA2 == 0)  || (PORTAbits.RA3 == 0)){
cnt++;
}

switch(cnt){
case 1: LED1o;LED8f;break;case 2: LED2o;LED1f;break;
case 3: LED3o;LED2f;break;case 4: LED4o;LED3f;break;
case 5: LED5o;LED4f;break;case 6: LED6o;LED5f;break;
case 7: LED7o;LED6f;break;case 8: LED8o;LED7f;break;
}
 Delay10KTCYx(200);

}
}


.....also i seen recent questions on demoboards, i do it this way so then i dont need to plan for icsp bus, and can eject the chip for programming(when icsp clk/dat debugging conflict with the port data(not in this case today))...... just my idea of simplicity!
IMG_20131204_121949.jpg
 
Maybe you are pressing the button too fast, and therefore it goes undetected (because you press the button during the delay).

You need to simplify your code until it starts working. Make a simple LED-blinker first etc.

The hardware looks also strange. Shouldn't the PIC be attached to the PCB? :)
Are you sure this is a software problem?
 
lol, it is assembled during testing, that was just for the photo, in image you see my circuit about to be programmed to right, and my test pic mounted on a "bug bed" with icsp pins soldered in and going to pickit on left.


im sorry if i confused 2 separate issws above, leds are ok,

it is the reading of porta0 and a1 that is not happening,
watch should be reading 1's when buttons are up, but are not,
breakpoint is occureing @cnt++; even when all buttons are up, which it should not..
port a1 is a button just like b0,a2,a3
port a0 is pot but should still give logic level values no?
even when simplified, leds were blinding anyway:

///#include <adc.h>
#include <p18f1230.h>
#include <string.h>
#include <delays.h>

void main(){

unsigned char cnt;

OSCCON= 0b01110000; //8MHz please
OSCTUNE=0b00011100;
CMCON=0x00;

TRISA = 0b00111111;
TRISB = 0b00000001;
cnt = 0;

while(1) {

if ((PORTBbits.RB0 == 0) || (PORTAbits.RA0 == 0) ||
(PORTAbits.RA2 == 0) || (PORTAbits.RA3 == 0)){
cnt++;
}}}
 
So, you have pulldown resistors in the button pins, and when you press a button that pin is connected to 5v?

Also, your new code reads different ports that your first code (the if-statement is different).. what are the correct pins and how are they connected?
 
Simplify the code so that you read only one button. Then if that works, test each button individually.
 
other way around like this: 5v--resistor--picinput--button-- ground,
button pulls down,
when button is up we get 5v in circuit,
additional simplification failed....
watch the watch.jpg
 
Last edited:
additional simplification failed....

How do you know it failed? Your code (the screenshot) does not show any led-pins being written.
 
watch tells me in debugging?

ok.. so you have been simulating all the time. You should have mentioned that. Maybe the simulator has a bug. At least turn off compiler optimizations.

Do you change the value of pin RA1 manually and then simulate?

You need to test the code in both situations. When RA1 = 0 and when RA1 = 1
 
never seen that b4, are u talking about switching to release mode, or just not trusting watch?
that doesnt work either.
a1 is pulled up to 5v with 10k,

I should maybe mention, i blame code since when i get these problems it is usually cause the pin i used is running comparator or a osc thing that gets in my way , but a1 has nothing like that in datasheet, and a2,a3 work ok, google says to check to see if readan is in the way but i cut that out already aswell,
 
a1 is pulled up to 5v with 10k,

You can do that in your simulator? I would not trust that.
You can clearly see that the A1 is not high.
 
Test the same code as shown in the screenshot in post #6, But this time change the if statement to:

if(PORTAbits.RA1 == 1){
cnt++;
}

See if the result is different (cnt should not count).
 
wait,wha, no that is in real circuit:),
and you are rite, with your code count does not count, but button down does not count either

but all this talk has found something that maybe interesting, i have added code to read the ports(a0 & a1) as analog and the data is going through ok, it is reading the pot and the button values ok, but when i delete the adc stuff it does not go to digital input...
it is enough that now i can get circuit going but i wonder why i can't read digital...
 
and you are rite, with your code count does not count, but button down does not count either

The first simulation shows that the "cnt" value has changed from 0 to 5 when RA1 is 0.. so It does count when button is down.
See the "0x05"-value in red font in the screenshot?
 
yes, bc 0v is being detected regardless, even when there is actually 5v @ pin, so it doesnt matter if button is actually down or up
 
yes, bc 0v is being detected regardless, even when there is actually 5v @ pin, so it doesnt matter if button is actually down or up

You are talking about simulator. You can't apply voltages to pins when simulating. Or is this an onchip-simulation/debugging (jtag.. or something)?
 
no simulation i know of , i am using in circuit debugging for mplab,

Ok.. You should have mentioned that also. I'm starting to waste time here with you. What else important have you not told me? Do I need to play this guessing game, or could you please tell me the setup.
 
sry, i was not aware, thought you meant debugging watch when you said simulation, did you see 2 posts back, about the analog stuff, i can read analog responses from the button, but i have since deleted adc.h and such, but still the inputs does not go to digital
 
Status
Not open for further replies.

Latest threads

Back
Top