Electronics > Projects, Designs, and Technical Stuff

thoughts on my project

<< < (8/12) > >>

ali6x944:
ok so here is the test code:

--- Code: ---/*MAIN LOOP*/
void main(void)
{
    OSCFRQ =0b101; // OSCFRQ(HFFRQ0-2) SET OSCILLATOR TO 16MHz
    while(1)
    {
        TRISAbits.TRISA0=0;
        LATAbits.LATA0=1;
        __delay_ms(500);
        TRISAbits.TRISA0=1;
        __delay_ms(500);
        TRISAbits.TRISA0=0;
        LATAbits.LATA0=0;
        __delay_ms(500);
        TRISAbits.TRISA0=1;
        __delay_ms(500);
    }
--- End code ---
and here is a scope capture of the RA0 pin:

ali6x944:
do I test the other ones?
cus it seems to work...
however in when TRISA is 1, the chip gives around 60 Hz 500mv pk-pk ripple I don't know if that is normal, I suspect it is just interference coming through the mains because my scope is not earthed...

Prehistoricman:
That looks great. The interference is actually great here because you can clearly see when the pin is high-impedance.
So let's try reading that pin when it's configured as an input:


--- Code: ---#include <stdbool.h>
/*MAIN LOOP*/
void main(void)
{
    OSCFRQ =0b101; // OSCFRQ(HFFRQ0-2) SET OSCILLATOR TO 16MHz
    bool read;
    while(1)
    {
        TRISAbits.TRISA0=0; //output
        LATAbits.LATA0=read;
        __delay_ms(500);
       
        TRISAbits.TRISA0=1; //input
        __delay_ms(100);
        read = PORTAbits.RA0;
        __delay_ms(400);
    }
}

--- End code ---

This should set the output to be the same value as the input. Try pulling the pin low or high with a resistor (don't short it) and see if the output phase follows the input value.

Prehistoricman:
Hold up.
I found this amazing information source: https://www.microforum.cc/topic/6-what-is-the-difference-between-port-x-and-lat-x-on-pic16-and-pic18-microcontrollers-and-what-does-anselx-actually-do/
This could certainly be related to your issue.
The main takeaways are:

Don't use PORTxbits for writing to pins, use LATxbits
Set ANSELxbits to 0 on all pins you want to read from

But the whole thing is a good read to know why these pieces of advice hold.
So try your pin-reading demos again with these changes.

ali6x944:

--- Quote ---Summary

So, in summary, the purpose of the LATx registers is to allow us to easily mask bits into our output ports without having to worry about read-modify-write problems. A general rule of thumb is to always read and write the LATx registers if you are trying to manipulate outputs on a PORT and always read the PORT registers only in the case where you are trying to get the digital input values of the pins on the port.
--- End quote ---
well, this is kinda what we did in the beginning, the only difference now is I had to specify all ANSELx register bits which I assumed where zero unless otherwise manipulated...

--- Code: ---#define BIG_SW PORTAbits.RA4 //READ RA4 AND ASSIGN IT TO BIG_SW
#define SMALL_SW PORTAbits.RA5 //READ RA5 AND ASSIGN IT TO SMALL_SW

/*MAIN LOOP*/
void main(void)
{
    OSCFRQ =0b101; // OSCFRQ(HFFRQ0-2) SET OSCILLATOR TO 16MHz
   
    /*TRISx AND ANSELx ASSIGNMENT */
    TRISAbits.TRISA0=0; // TRISA0(RA0) BIG LIGHT DRIVE, OUTPUT
    TRISAbits.TRISA1=0; // TRISA1(RA1) SMALL LIGHT DRIVE, OUTPUT
    TRISAbits.TRISA2=1; // TRISA2(RA2) ANALOG POT, INPUT
    TRISAbits.TRISA4=1; // TRISA4(RA4) BIG LIGHT SWITCH, INPUT
    TRISAbits.TRISA5=1; // TRISA5(RA5) SMALL LIGHT SWITCH, INPUT
    ANSELAbits.ANSA0=0; // ANSELA0(ANSA0) DISABLED, INPUT DIGITAL
    ANSELAbits.ANSA1=0; // ANSELA0(ANSA1) DISABLED, INPUT DIGITAL
    ANSELAbits.ANSA2=1; // ANSELA2(ANSA2) ANALOG POT, INPUT ANALOG
    ANSELAbits.ANSA4=0; // ANSELA4(ANSA4) DISABLED, INPUT DIGITAL
    ANSELAbits.ANSA5=0; // ANSELA5(ANSA5) DISABLED, INPUT DIGITAL
   
    LATAbits.LATA1 = 0;
    LATAbits.LATA0 = 0;
   
    while(1)
    {
        if(BIG_SW)
        {
            LATAbits.LATA0 = 1; //LED ON
           
        }
        if(SMALL_SW)
        {
            LATAbits.LATA1 = 1; //LED ON
           
        }
        else
        {
        LATAbits.LATA1 = 0;
        LATAbits.LATA0 = 0;
        }
    }
   
}
--- End code ---

so, this is my sanity check, and it seems to work well, however, if both switches are high or low dose not go into the else condition...
I later added these and it now goes to the else condition if both are low but when both are high the output is still high for some reason:

--- Code: ---#define BIG_SW PORTAbits.RA4 //READ RA4 AND ASSIGN IT TO BIG_SW
#define SMALL_SW PORTAbits.RA5 //READ RA5 AND ASSIGN IT TO SMALL_SW

/*MAIN LOOP*/
void main(void)
{
    OSCFRQ =0b101; // OSCFRQ(HFFRQ0-2) SET OSCILLATOR TO 16MHz
   
    /*TRISx AND ANSELx ASSIGNMENT */
    TRISAbits.TRISA0=0; // TRISA0(RA0) BIG LIGHT DRIVE, OUTPUT
    TRISAbits.TRISA1=0; // TRISA1(RA1) SMALL LIGHT DRIVE, OUTPUT
    TRISAbits.TRISA2=1; // TRISA2(RA2) ANALOG POT, INPUT
    TRISAbits.TRISA4=1; // TRISA4(RA4) BIG LIGHT SWITCH, INPUT
    TRISAbits.TRISA5=1; // TRISA5(RA5) SMALL LIGHT SWITCH, INPUT
    ANSELAbits.ANSA0=0; // ANSELA0(ANSA0) DISABLED, INPUT DIGITAL
    ANSELAbits.ANSA1=0; // ANSELA0(ANSA1) DISABLED, INPUT DIGITAL
    ANSELAbits.ANSA2=1; // ANSELA2(ANSA2) ANALOG POT, INPUT ANALOG
    ANSELAbits.ANSA4=0; // ANSELA4(ANSA4) DISABLED, INPUT DIGITAL
    ANSELAbits.ANSA5=0; // ANSELA5(ANSA5) DISABLED, INPUT DIGITAL
   
    LATAbits.LATA1 = 0;
    LATAbits.LATA0 = 0;
   
    while(1)
    {
        if(BIG_SW)
        {
            LATAbits.LATA0 = 1; //LED ON
            LATAbits.LATA1 = 0; //LED off
           
        }
        if(SMALL_SW)
        {
            LATAbits.LATA1 = 1; //LED ON
            LATAbits.LATA0 = 0; //LED off
        }
        else
        {
        LATAbits.LATA1 = 0;
        LATAbits.LATA0 = 0;
        }
    }
   
}
--- End code ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod