2

I'm learning PIC (pic18f4550) and pretty new to microcontroller programming. I'm trying get value of three button on PORTA and send it to a 8x8 led matrix as X coordinates through a 74LS595. The problem is that the value go to the led matrix doesnt change when i pressed the buttons to create different value. I'm simulating on Proteus so I guess I don't need debounce function. Here's my code and schematic:

#include<p18f4550.h>

#define SCK LATBbits.LATB0
#define DATA PORTBbits.RB1
#define SCL PORTBbits.RB2

void Data_in(unsigned char k){
    DATA=k;
    SCK=0;
    SCK=1;
}

void LatchData(){
    SCL=0;
    SCL=1;
}

void Send1byte(unsigned char data)
{
    unsigned char i,temp;
    for(i=0;i<8;i++)
    {
        temp = data & (1<<i);
        if(temp)
        {
            DATA = 1;
        }
        else
        {
            DATA = 0;
        }

        SCK = 0;
        SCK = 1;
    }

SCL = 0;
SCL = 1;
}

unsigned char getMatrixX(unsigned char in_X)
{

    switch(in_X)
    {
        case 0:      // the value stuck here
            return 0b01111111;
        case 1:
            return 0b10111111;
        case 2:
            return 0b11011111;
        case 3:
            return 0b11101111;
        case 4:
            return 0b11110111;
        case 5:
            return 0b11111011;
        case 6:
            return 0b11111101;
        case 7:
            return 0b11111110;
        default:
            return 0b11111111;
    }
}

void main()
{

    TRISA = 1;
    TRISC = 1;

    TRISB = 0;
    TRISD = 0;

    PORTD = 0x80;

    while(1){
        Send1byte(getMatrixX(LATA));
    }
}

This is link to my schematic: my schematic

Really appreciate any solutions and advices. Sorry for my bad english.

  • Should `TRISA = 1;` be something like `TRISA = 0xFF;`? – chux - Reinstate Monica May 23 '14 at 17:45
  • Thank you, I tried, it still not work, besides, my text book said it's ok to write TRISA =1; – user3669754 May 23 '14 at 17:49
  • Have you isolated the problem to the input function or the output LED matrix? Like if you have `getMatrixX` always return `0b11111101`, does it light up the matrix? – indiv May 23 '14 at 18:37
  • yes, the problem is exactly the input, if i put random value by code to the function Send1byte, the matrix does light up correctly, but if I get value from button and put it in getMatrixX function, it stuck at 0b01111111 like the picture i gave – user3669754 May 23 '14 at 18:44
  • I can't look at the picture. Are you supposed to be reading `PORTA`? I am seeing that `LATA` [reads the latch register](http://stackoverflow.com/questions/2623428/difference-between-port-and-latch-on-pic-18f), which is not necessarily the value of the I/O pins. Also, your LED matrix will only light up while the button is held down the way your code is now so make sure the button is not transitioning too fast since you're using a simulator. If none of that helps, then trace the signals on the I/O pins and make sure they're really changing when you press a button. – indiv May 23 '14 at 19:09
  • have u had a look at [the docs](http://ww1.microchip.com/downloads/en/DeviceDoc/39632e.pdf), p. 279, p. 113 ff.? Since you're using pullups, the unpressed state of the connected inputs should read 1 after initializing TRISA/C with 0x07. Also, LATC needs to be mixed into Send1byte(getMatrixX(LATA)); somehow. – wonko realtime May 23 '14 at 19:17
  • indiv, proteus allow me to keep the state of buttons during simulation (pressed or unpressed) so I just want to read their states as input for my function, about LAT, you are right, i should read from PORT, but I tried, it still doesn't work – user3669754 May 23 '14 at 19:35
  • wonko, yes, its state is 1 when unpressed.About the A and C, I meant to use just PORTA for input to the HC595, the port C is for later experiment, thanks for your comment anyway :) – user3669754 May 23 '14 at 19:39

1 Answers1

1

the analogue function of RA0:RA3 is the real problem here, so adding these will fix:

ADCON1 = 0x0F; // All digital inputs
CMCON = 0x07;  // Comparators off (note this is the POR default)

Thanks David in this question: https://electronics.stackexchange.com/questions/111614/pic-programming-get-value-of-multiple-buttons-to-a-port/111625?noredirect=1#111625 , he explained it very well.

Community
  • 1
  • 1