Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

Why doesn't my INT change values like it should?

Status
Not open for further replies.

cartman007

Member level 2
Joined
Nov 13, 2012
Messages
45
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,638
Hey Guys

Yesterday night I messed around with a tutorial and after allot of editing to make it work for me it still isn't doing anything.
Now I had to change allot of the code like the delays I had to change cause my version of "delays.h" seems to be doing something different then the normal delay_ms(20); etc...

What its suppose to do is take a reading on port A0 from a resistor and then just adjust the LEDs according to what it receives.
But whats happening now is the LED on port RB2 keeps on shining no matter what I do even if the function that makes the LEDs shine is commented out it still shines.
AND if I Breakpoint my function the "avg_out" integer of the function just seems to stay at "0x2048" and immediately switch to "0x0000" upon entering the function BUT I dont see how this explains the 1 LED burning.

Heres my Code

Code:
// PIC18F4550 Configuration Bit Settings

#include <p18F4550.h>

// CONFIG1L
#pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)

// CONFIG1H
#pragma config FOSC = INTOSC_HS // Oscillator Selection bits (Internal oscillator, HS oscillator used by USB (INTHS))
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON         // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting)
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)





// Program to depict working with inbuilt ADC of PIC18F4550 Microcontroller
// This code uses Channel0 (zero) of PIC's ADC Module

// Configuration bits
/* _CPUDIV_OSC1_PLL2_1L,  // Divide clock by 2
   _FOSC_HS_1H,           // Select High Speed (HS) oscillator
   _WDT_OFF_2H,           // Watchdog Timer off
   MCLRE_ON_3H            // Master Clear on
*/

#include <p18f4550.h>
#include <delays.h>
#include <ADC.h>
#include <p18cxxx.h>

#define rs LATA.F0
#define rw LATA.F1
#define en LATA.F2
#define lcdport LATB



void adc_con(unsigned int);
void adc_init();
void LEDs(int avg_out);

unsigned char data[20]="ADC OUTPUT=";
unsigned int digital_out[10],avg_output=0,temp;
unsigned int i=0;


void main()
{
    TRISA=0x01;        // Configure RA0 as input pin
    LATA=0;
    TRISB=0;        // Configure Port B as output port
    LATB=0;
    TRISD=0;
    LATD=0;
    TRISD=0;


    adc_init();        //ADC Initialization
    PORTD=00000010;
    while(1)
    {
        temp=0;
        for(i=0;i<10;i++)
        {
            ADCON0|=(1<<ADCON0bits.GO);                              // Start A/D conversion
            while(!(ADCON0 & (1<<ADCON0bits.GO)));                   // Wait until conversion gets over
            digital_out[i]=((ADRESL)|(ADRESH<<8));        // Store 10-bit output into a 16-bit variable
            Delay1KTCYx (240);
            temp=temp+digital_out[i];
        }
        avg_output=temp/10;                           // Take average of ten digital values for stablity
        //adc_con(avg_output);                          // Function to convert the decimal vaule to its corresponding ASCII

        LEDs(avg_output);

    }
}

/*void LEDs(int avg_out)
{   
    if (avg_out <= 50)
    {PORTB = 00000001;}

    if (avg_out >= 50)
    {PORTB = 00000010;}
    
    if (avg_out >= 100)
    {PORTB = 00000011;}
    
    if (avg_out >= 150)
    {PORTB = 00000111;}
    
    if (avg_out >= 200)
    {PORTB = 00001111;}
    
    if (avg_out >= 300)
    {PORTB = 00111111;}
    
    if (avg_out >= 400)
    {PORTB = 01111111;}
    
    if (avg_out >= 490)
    {PORTB = 11111111;}
}
*/

void LEDs(int avg_out)
{
    if ( avg_out > 0 && avg_out <= 100)
    {PORTB = 00000010;}
    else
    if ( avg_out > 100 && avg_out <= 200)
    {PORTB = 00000001;}
    else
    if ( avg_out > 200 && avg_out <= 300)
    {PORTB = 00000011;}
    else
     if ( avg_out > 300 && avg_out <= 400)
    {PORTB = 00000111;}
     else
     if ( avg_out > 400 && avg_out <= 500)
     {PORTB = 00000100;}

   /* if (avg_out >= 200)
    {PORTB = 00001111;}

    if (avg_out >= 300)
    {PORTB = 00111111;}

    if (avg_out >= 400)
    {PORTB = 01111111;}

    if (avg_out >= 490)
    {PORTB = 11111111;}*/
}




void adc_init()
{
    ADCON1=0x0E;                            // Make RA0/AN0 pin as analog pin (Other pins remain to be digital I/O)
    ADCON0=0x00;                            // Select Channel0 & ADC off
    ADCON2=0x8A;                            // Left justified, 2TAD acquiciation time, Fosc/32 clock option
    ADCON0bits.ADON=1;                          // Enable ADC
}




void adc_con(unsigned int adc_out)
{
    unsigned int adc_out1;
    int i=0;
    char position=0xC3;

    for(i=0;i<=3;i++)
    {
        adc_out1=adc_out%10;                     // To exract the unit position digit
        adc_out=adc_out/10;
        position--;

    }
}
 

BradtheRad

Super Moderator
Staff member
Joined
Apr 1, 2011
Messages
13,794
Helped
2,735
Reputation
5,466
Reaction score
2,645
Trophy points
1,393
Location
Minneapolis, Minnesota, USA
Activity points
102,964
I have not used this programming language, but in BASIC I find it's very easy to forget that an integer gets rounded when mixing it with floating point numbers. You can add or subtract fractions from it all day, and it stays the same.

I also find that it is very tricky when you mix signed and unsigned variables in calculations, or with relational operators such as < or >.

Sometimes it's easiest just to use floating point (as long as there's room in memory for the necessary bytes).
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top