Author Topic: thoughts on my project  (Read 11029 times)

0 Members and 1 Guest are viewing this topic.

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #25 on: March 15, 2020, 02:33:03 pm »
it didn't work at all  |O
IDK WTF is going on...
maybe I messed something up in my ccp register code or it is not leaving sleep
Code: [Select]
/***************************************************************************************************************************
 *
 * Author:Ali6x944
 * Date: 8/1/2020
 * Device: PIC16LF15313
 * Description: A program designed to run on PIC16LF15313, that facilitates a single-chip
 * solution to controlling 2 LED Panels/Arrays using a single multi-position slider switch and a pot to control brightness.
 *   
 ***************************************************************************************************************************/
// PIC16LF15313 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF    // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1  // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = OFF      // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

// CONFIG2
#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = OFF      // Brown-out reset enable bits (Brown-out reset disabled)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF       // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

// CONFIG4
#pragma config BBSIZE = BB512   // Boot Block Size Selection bits (512 words boot block size)
#pragma config BBEN = OFF       // Boot Block Enable bit (Boot Block disabled)
#pragma config SAFEN = OFF      // SAF Enable bit (SAF disabled)
#pragma config WRTAPP = OFF     // Application Block Write Protection bit (Application Block not write protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block not write protected)
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration Register not write protected)
#pragma config WRTSAF = OFF     // Storage Area Flash Write Protection bit (SAF not write protected)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

// CONFIG5
#pragma config CP = OFF         // UserNVM Program memory code protection bit (UserNVM code protection disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <limits.h>
#include <stdint.h>
/*VARIABLE DEFFENITION SECTION*/
uint16_t RESULT; // ADC RESULT VARIABLE
__bit BIG_SW; // VARIABLE INDICATING STATE OF BIG_SWITCH CONTROLLING BIG PANEL
__bit SMALL_SW; // VARIABLE INDICATING STATE OF SMALL_SWITCH CONTROLING SMALL PANEL

/*FUNCTION INTITILIZATION SECTION*/
void ADC_RESULT()
    {
        ADCON0bits.GOnDONE=1; // ADC CONVERSION STATUS BITS
        while(ADCON0bits.GOnDONE==1); // WAIT WHILE ADC STATUS IS ON
        RESULT = ADRESH << 8; //Load ADC high value
        RESULT |= ADRESL; //Load ADC low value
    }

/*MAIN LOOP*/
int main(void)
{
    OSCFRQ=0b010; // OSCFRQ(HFFRQ0-2) SET OSCILLATOR TO 4MHz
   
    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.ANSA2=1; // ANSELA2(ANSA2) ANALOG POT, INPUT ANALOG
    CCP1PPSbits.CCP1PPS=0b00000; // BIG LIGHT DRIVER (PWM->RA4), OUTPUT
    CCP2PPSbits.CCP2PPS=0b00001; // SMALL LIGHT DRIVER (PWM->RA5), OUTPUT
   
    ADCON0bits.CHS=0b000010; // ANALOG CHANNEL SELECTION BITS (RA2)
    ADCON1bits.ADCS=0b111; // ADC COVERSION CLOCK SELECTION BITS (ADCRC)
    ADCON0bits.ADON=1; // ANALOG CHANNEL SELECTION BITS (ADCON HIGH)
    ADCON1bits.ADPREF=0b00; //ADC POSITIVE VOLTAGE REFRENCE CONFIGRATION bits (VREF+ IS VDD)
    ADCON1bits.ADFM=1; // ADC RESULT FORMAT SELECTION BITS (RIGHT JUSTIFIED)
   
    BIG_SW=PORTAbits.RA4; //READ RA4 AND ASSIGN IT TO BIG_SW
    SMALL_SW=PORTAbits.RA5; //READ RA5 AND ASSIGN IT TO SMALL_SW
   
    PIE0bits.IOCIE=1;//INTERUPT-ON-CHANGE ENABLE BIT IS SET
    IOCANbits.IOCAN4=1;//INTERUPT-ON-CHANGE PORTA (RA4) NEGATIVE EDGE TRIGGERED
    IOCANbits.IOCAN5=1;//INTERUPT-ON-CHANGE PORTA (RA5) NEGATIVE EDGE TRIGGERED
   
    while(1)
    {
        while(BIG_SW==1||SMALL_SW==1)
        {
        PMD1bits.TMR2MD=0b0; // TIMER2 MOUDLE ENABLED
        T2CONbits.CKPS=0b000; //TIMER2 PRESCALER SET TO 1:1
        T2CONbits.OUTPS=0b0000; //TIMER2 POST-SCALER SET TO 1:1
        T2PRbits.T2PR=0b01100011;  // PR2 REGESTER SET TO 99
        T2CLKCONbits.CS=0b0011; // TIMER2 CLOCK SOURCE SELECT BIT (HFINTOSC)
       
        CCP1CONbits.CCP1FMT=1; //CCPW PULSE WIDTH Alignment bit (RIGHT JUSTIFIED)
        CCP1CONbits.CCP1MODE=0b1111; // MODE IS SET TO PWM
        CCP2CONbits.CCP2FMT=1; //CCPW PULSE WIDTH Alignment bit (RIGHT JUSTIFIED)
        CCP2CONbits.CCP2MODE=0b1111; // MODE IS SET TO PWM
       
        if(BIG_SW==1)
        {
            ADC_RESULT();
            CCPR1=RESULT;
            PIR4bits.TMR2IF=0b0;
            T2CONbits.T2ON=0b1;
        }
       
        if(SMALL_SW==1)
        {
            ADC_RESULT();
            CCPR2=RESULT;
            PIR4bits.TMR2IF=0b0;
            T2CONbits.T2ON=0b1;
        }
       
        }
        if (BIG_SW==0&&SMALL_SW==0)
        {
            __asm__("SLEEP");
        }
   
   
    }
   
   
   
 
}

Edit:
I just removed the sleep section and it does not seem to work...
this is the part that I removed:
Code: [Select]
if (BIG_SW==0&&SMALL_SW==0)
        {
            __asm__("SLEEP");
        }
« Last Edit: March 15, 2020, 02:41:17 pm by ali6x944 »
 

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #26 on: March 15, 2020, 03:38:34 pm »
Are you sure you ever enter sleep?
I keep telling you that you aren't updating the BIG_SW and SMALL_SW variables, but you aren't making any changes. Why?!
I suggest you get your code working, forgetting about sleep (because your LED panels will consume far more power than the microcontroller). Work up from something that works.

If you want to fix sleep, here's some ideas:
You might need an interrupt handler for the interrupts you just enabled. With nothing to reset the interrupt request, the micro will not trigger additional interrupts.
Blink an LED while the CPU is active. It will stop blinking when it goes to sleep.
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #27 on: March 15, 2020, 03:49:50 pm »
Are you sure you ever enter sleep?
I keep telling you that you aren't updating the BIG_SW and SMALL_SW variables, but you aren't making any changes. Why?!
I suggest you get your code working, forgetting about sleep (because your LED panels will consume far more power than the microcontroller). Work up from something that works.
cool, so how do I do that? I mean the BIG_SW and SMALL_SW variables continues update
 

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #28 on: March 15, 2020, 08:07:44 pm »
Well you already update them once:

Code: [Select]
BIG_SW=PORTAbits.RA4; //READ RA4 AND ASSIGN IT TO BIG_SW
SMALL_SW=PORTAbits.RA5; //READ RA5 AND ASSIGN IT TO SMALL_SW

So just run this inside the main loop.
Your comments kind of highlights the misunderstanding. It's not 'assigned'. The value of the pins is read and that value is put into the variable.

Consider this example:

x = 2
y = x
x = 3

What is y?
In normal languages, it will be 2. The assignment of x to y means "the value of x is copied to y". If you only run the copy once, you get one copy.
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #29 on: March 19, 2020, 08:18:58 am »
ok so I think I did what u meant, and it didn't work, so I overdid it for good measure and it didn't work  |O
Code: [Select]
/*MAIN LOOP*/
int main(void)
{
    OSCFRQ=0b010; // OSCFRQ(HFFRQ0-2) SET OSCILLATOR TO 4MHz
   
    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.ANSA2=1; // ANSELA2(ANSA2) ANALOG POT, INPUT ANALOG
    CCP1PPSbits.CCP1PPS=0b00000; // BIG LIGHT DRIVER (PWM->RA4), OUTPUT
    CCP2PPSbits.CCP2PPS=0b00001; // SMALL LIGHT DRIVER (PWM->RA5), OUTPUT
   
    ADCON0bits.CHS=0b000010; // ANALOG CHANNEL SELECTION BITS (RA2)
    ADCON1bits.ADCS=0b111; // ADC COVERSION CLOCK SELECTION BITS (ADCRC)
    ADCON0bits.ADON=1; // ANALOG CHANNEL SELECTION BITS (ADCON HIGH)
    ADCON1bits.ADPREF=0b00; //ADC POSITIVE VOLTAGE REFRENCE CONFIGRATION bits (VREF+ IS VDD)
    ADCON1bits.ADFM=1; // ADC RESULT FORMAT SELECTION BITS (RIGHT JUSTIFIED)
   
    PIE0bits.IOCIE=1;//INTERUPT-ON-CHANGE ENABLE BIT IS SET
    IOCANbits.IOCAN4=1;//INTERUPT-ON-CHANGE PORTA (RA4) NEGATIVE EDGE TRIGGERED
    IOCANbits.IOCAN5=1;//INTERUPT-ON-CHANGE PORTA (RA5) NEGATIVE EDGE TRIGGERED
   
    while(1)
    {
        BIG_SW=PORTAbits.RA4; //READ RA4 AND ASSIGN IT TO BIG_SW
        SMALL_SW=PORTAbits.RA5; //READ RA5 AND ASSIGN IT TO SMALL_SW 
        while(BIG_SW==1||SMALL_SW==1)
        { 
        PMD1bits.TMR2MD=0b0; // TIMER2 MOUDLE ENABLED
        T2CONbits.CKPS=0b000; //TIMER2 PRESCALER SET TO 1:1
        T2CONbits.OUTPS=0b0000; //TIMER2 POST-SCALER SET TO 1:1
        T2PRbits.T2PR=0b01100011;  // PR2 REGESTER SET TO 99
        T2CLKCONbits.CS=0b0011; // TIMER2 CLOCK SOURCE SELECT BIT (HFINTOSC)
       
        CCP1CONbits.CCP1FMT=1; //CCPW PULSE WIDTH Alignment bit (RIGHT JUSTIFIED)
        CCP1CONbits.CCP1MODE=0b1111; // MODE IS SET TO PWM
        CCP2CONbits.CCP2FMT=1; //CCPW PULSE WIDTH Alignment bit (RIGHT JUSTIFIED)
        CCP2CONbits.CCP2MODE=0b1111; // MODE IS SET TO PWM
       
        if(BIG_SW==1)
        {
            BIG_SW=PORTAbits.RA4; //READ RA4 AND ASSIGN IT TO BIG_SW
            SMALL_SW=PORTAbits.RA5; //READ RA5 AND ASSIGN IT TO SMALL_SW 
            ADC_RESULT();
            CCPR1=RESULT;
            PIR4bits.TMR2IF=0b0;
            T2CONbits.T2ON=0b1;
        }
       
        if(SMALL_SW==1)
        {
            BIG_SW=PORTAbits.RA4; //READ RA4 AND ASSIGN IT TO BIG_SW
            SMALL_SW=PORTAbits.RA5; //READ RA5 AND ASSIGN IT TO SMALL_SW 
            ADC_RESULT();
            CCPR2=RESULT;
            PIR4bits.TMR2IF=0b0;
            T2CONbits.T2ON=0b1;
        }
       
        }
       
   
   
    }
   
   
   
 
}
I tried all combinations of the main loop and the subloops but it didn't work, I even got a new chip just to check I am not crazy and it doesn't seem to work  :-//
just as a form of sanity check, I went and removed the ccp and ADC stuff and just used the LATx register to output a high if its corresponding switch is high and it still didn't work  :horse:
so just to check that my chip isn't dead or something, I got a new one and tried it out and it didn't work with and without the trimmed code  :-BROKE
so I trimmed the whole program down to a just blink a led at lata0 and it finally worked  :phew:
at this point, I have to follow an empirical approach, so I rewrote my program around the functioning blink program...
so here is a rundown of all the code that seems to be screwing with the operation of the code:
1)
Code: [Select]
OSCFRQ =0b010; // OSCFRQ(HFFRQ0-2) SET OSCILLATOR TO 4MHz
apparently, this code seems to slow down the CPU for some reason so I fixed it by increasing the clock:
Code: [Select]
OSCFRQ =0b101; // OSCFRQ(HFFRQ0-2) SET OSCILLATOR TO 16MHz
I also add a #define _XTAL_FREQ 4000000 to make the delay function work and will be removed later...

2)
reading and updating big switch, however, proved to be annoyingly difficult, I tried to do it inside the main loop, outside the main loop, in one IOC ISR, in two sperate IOC ISR, to the point of absolute  insanity :scared:
at this point I admit couldn't conditionally toggle a GPIO on or off if my life depended on it  :'(

3)
for both the ccp and ADC sanity check I had to rely on some conditional statement and let's just say that didn't go very well :palm:



 

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #30 on: March 19, 2020, 12:04:58 pm »
Damn, that really sucks.
Maybe you can try using the whole-port names instead of bitfields. Like:

TRISA = 0x034; //RA0, 1 output. RA2, 4, 5 input
(PORTA & 0x10) > 0 //Reads RA4
(PORTA & 0x20) > 0 //Reads RA4
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #31 on: March 19, 2020, 12:22:11 pm »
Damn, that really sucks.
Maybe you can try using the whole-port names instead of bitfields. Like:

TRISA = 0x034; //RA0, 1 output. RA2, 4, 5 input
(PORTA & 0x10) > 0 //Reads RA4
(PORTA & 0x20) > 0 //Reads RA4
give me a minute...

Edit:
that doesn't seem to work  :-//
« Last Edit: March 19, 2020, 12:24:19 pm by ali6x944 »
 

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #32 on: March 19, 2020, 08:17:33 pm »
Just to clarify: does 'doesn't work' mean that it compiles fine but doesn't run fine?

Have you checked that the pins that you're outputting to are functioning normally? Did you do the LED check on these ones?
Can you set an output to turn the LED on, then change its pin mode to be input? Does the LED go out? Do this in a loop with output both high and low, just for sanity checking.
How are you compiling this? Perhaps this is a compilation issue.
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #33 on: March 20, 2020, 10:22:16 am »
Just to clarify: does 'doesn't work' mean that it compiles fine but doesn't run fine?
exactly.
Have you checked that the pins that you're outputting to are functioning normally? Did you do the LED check on these ones?
yes, I did the LED check exclusively on the output pins (RA0, RA1), both timing and output voltage seem normal.
Can you set an output to turn the LED on, then change its pin mode to be input? Does the LED go out? Do this in a loop with output both high and low, just for sanity checking.
So what u want me to do is set (RA0, RA1) to be inputs? or am I missing something?
How are you compiling this? Perhaps this is a compilation issue.
didn't understand the question? do you mean the compiler type? it is XC8 v2.00
 

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #34 on: March 20, 2020, 11:47:52 am »
So what u want me to do is set (RA0, RA1) to be inputs? or am I missing something?

Yeah. These are the steps:

Set pin to be output
Set pin high
delay
Set pin to be input
delay
Set pin to be output
Set pin low
delay
Set pin to be input
delay
loop (do it all again)

When you configure the pin as an input, it should go high-impedance. So any LED connected will turn off. What you should see is that the LED is only turned on for one delay (set this to be 0.5s or 1s).
This test is to confirm that setting the pin as an input is working correctly.
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #35 on: March 21, 2020, 06:55:44 am »
ok so here is the test code:
Code: [Select]
/*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);
    }
and here is a scope capture of the RA0 pin:
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #36 on: March 21, 2020, 07:07:51 am »
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...
 

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #37 on: March 21, 2020, 12:50:11 pm »
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: [Select]
#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);
    }
}

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.

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #38 on: March 21, 2020, 02:11:53 pm »
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.
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #39 on: March 22, 2020, 05:21:10 pm »
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.
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: [Select]
#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;
        }
    }
   
}

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: [Select]
#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;
        }
    }
   
}
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #40 on: March 22, 2020, 05:44:39 pm »
now I found a weird thing also when I checked the signal on the scope:
1)PHOTO_1:
when RA0 is high and RA1 is low...
and for some reason, RA1 oscillates??
so I was kinda confused so I fired up channel 2 of my scope and here is what I got not that the blue is RA0 and the yellow RA1:
2)PHOTO_2:
is when both are on, they also oscillate.
3)PHOTO_3:
when RA1 is high and RA0 is low, no oscillation??? :wtf:
4)PHOTO_4:
when RA0 is high and RA1 is low, but this frame is with measurement.
 

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #41 on: March 22, 2020, 08:59:52 pm »
Code: [Select]
        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;
        }

This is where something is probably going wrong.
'else' only pairs up with one 'if'. So the first if statement is not exclusive with the second. In other words, both can happen.
You probably meant to have 'else if' on the second one.


now I found a weird thing also when I checked the signal on the scope:
1)PHOTO_1:
when RA0 is high and RA1 is low...
and for some reason, RA1 oscillates??
so I was kinda confused so I fired up channel 2 of my scope and here is what I got not that the blue is RA0 and the yellow RA1:
2)PHOTO_2:
is when both are on, they also oscillate.
3)PHOTO_3:
when RA1 is high and RA0 is low, no oscillation??? :wtf:
4)PHOTO_4:
when RA0 is high and RA1 is low, but this frame is with measurement.
To clarify, in this whole post you were talking about only RA0 and RA1, not RA4 and RA5 (your inputs). You don't mean that, right?

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #42 on: March 23, 2020, 05:33:01 am »

To clarify, in this whole post you were talking about only RA0 and RA1, not RA4 and RA5 (your inputs). You don't mean that, right?
correct, this is exclusively the output behavior of RA0 and RA1.
This is where something is probably going wrong.
'else' only pairs up with one 'if'. So the first if the statement is not exclusive with the second. In other words, both can happen.
You probably meant to have 'else if' on the second one
so I did that and it still doesn't enter the else condition:
Code: [Select]
while(1)
    {
        if(BIG_SW)
        {
            LATAbits.LATA0 = 1; //LED ON
            LATAbits.LATA1 = 0; //LED off
           
        }
        else if(SMALL_SW)
        {
            LATAbits.LATA1 = 1; //LED ON
            LATAbits.LATA0 = 0; //LED off
        }
        else
        {
        LATAbits.LATA1 = 0;
        LATAbits.LATA0 = 0;
        }
    }
now the oscilations are gone completly, and it turns off when both (RA4,RA5) are low, which is good.
however, it still dose not turn off when both (RA4,RA5) are high, instade RA0 goes high when both (RA4,RA5) are high.
 

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #43 on: March 23, 2020, 01:04:19 pm »
Your buttons probably short the pin to ground.
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #44 on: March 23, 2020, 03:05:29 pm »
Your buttons probably short the pin to ground.
not really, RA4 and RA5 are pulled to ground by 1k resistors and the buttons are jumper wires, so no it is not shorting, but it is always connected to ground via the 1k pulldown resistor...
 

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #45 on: March 23, 2020, 05:23:48 pm »
So they short to Vcc? Can you give a wiring diagram/schematic?

Notice you don't have a condition in the code for both buttons being high. BIG_SW has priority over SMALL_SW.
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #46 on: April 03, 2020, 04:41:24 pm »
so this the current wiring is attached below...
also, I have modified the code to solve the problem to this now:
Code: [Select]
/*MAIN FUNCTION*/
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
   
    /*LATx STEADY-STATE*/
    LATAbits.LATA1 = 0;
    LATAbits.LATA0 = 0;
   
    /*MAIN LOOP*/
    while(1)
    {
        if((BIG_SW&&SMALL_SW)||(!BIG_SW&&!SMALL_SW))
        {
            LATAbits.LATA0 = 0; //LED OFF
            LATAbits.LATA1 = 0; //LED OFF
           
        }
        else if(SMALL_SW)
        {
            LATAbits.LATA1 = 1; //LED ON
            LATAbits.LATA0 = 0; //LED OFF
        }
        else if (BIG_SW)
        {
            LATAbits.LATA1 = 0; //LED OFF
            LATAbits.LATA0 = 1; //LED ON
        }
    }
   
}
sorry for not checking it with the scope thou cus I had to do a bit of housekeeping...
soon however I will upload some.
 

Offline Prehistoricman

  • Regular Contributor
  • *
  • Posts: 216
  • Country: gb
Re: thoughts on my project
« Reply #47 on: April 04, 2020, 12:13:42 am »
Does it work as you expect now?

Btw, this line
Code: [Select]
if((BIG_SW&&SMALL_SW)||(!BIG_SW&&!SMALL_SW))
can be changed to
Code: [Select]
if (BIG_SW == SMALL_SW)
to make it more readable
 
The following users thanked this post: ali6x944

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #48 on: April 04, 2020, 12:08:30 pm »
Does it work as you expect now?

Btw, this line
Code: [Select]
if((BIG_SW&&SMALL_SW)||(!BIG_SW&&!SMALL_SW))
can be changed to
Code: [Select]
if (BIG_SW == SMALL_SW)
to make it more readable
yes, it does logically :-+
for the code modification, it works also, and it is implemented now.
I haven't checked the electrical profile of the signal at RA0, RA1 thou, will be checking it soon.
 

Offline ali6x944Topic starter

  • Frequent Contributor
  • **
  • Posts: 577
  • Country: sa
Re: thoughts on my project
« Reply #49 on: April 16, 2020, 02:36:12 pm »
hi again,
I have checked the electrical profile of the signal at RA0, RA1:
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf