You are not logged in.

#1 2010-11-20 19:10:52

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Microcontroller C (PIC)

I managed to get my PicKit 2 working with arch today using pk2cmd from the AUR.

I've been writing some C and programming my pic18f4520 using both the sdcc libraries (which are a little different than Microchip's) and the pk2cmd.

Everything has been going swimmingly except for this one thing; when I put the if statement into a function the displays show 1 and 1. When I put the if statement in main they show 3 and 4, which they are supposed too.

So, this works:

#include <pic16/pic18f4520.h>
#include <pic16/delay.h>
#include "display.h"

unsigned char number1, number2;
void main(void)
{
  displayInit();
  number1 = 3;
  number2 = 4;
  
  while (1)
  {
    if (U1_isOn())
    {
      switchOff_U1();
      displayOn_U2(number1);
    }
    else if (U2_isOn())
    {
      switchOff_U2();
      displayOn_U1(number2);
    }
  }
}

but this doesn't:

#include <pic16/pic18f4520.h>
#include <pic16/delay.h>
#include "display.h"

unsigned char number1, number2;

void display(unsigned char U1_value, unsigned char U2_value)
{
  if (U1_isOn())
  {
    switchOff_U1();
    displayOn_U2(U2_value);
  }
  else if (U2_isOn())
  {
    switchOff_U2();
    displayOn_U1(U1_value);
  }
}

void main(void)
{
  displayInit();
  number1 = 3;
  number2 = 4;
  
  while (1)
  {
    display(number1, number2)
  }
}

Does anyone have any idea?

Also: the display function is actually from the display.c file (that's what my header file is for). I've tried moving the whole thing into the main file but it still doesn't work.

Offline

#2 2010-11-21 00:52:38

juster
Forum Fellow
Registered: 2008-10-07
Posts: 195

Re: Microcontroller C (PIC)

The only difference I can see is that you are passing number2 (4) to displayOn_U1() and passing number1 (3) to displayOn_U2() in your working example. The non-working example does the opposite.

More generically: try printing out the arguments you are about to pass to the displayOn functions to verify what you believe you are passing it.

Offline

#3 2010-11-21 01:24:32

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: Microcontroller C (PIC)

This code, prints out:

Left | Right
   3 |
     | 2
   1 |
     | 1
#include <pic16/pic18f4520.h>
#include <pic16/delay.h>
#include "display.h"

volatile unsigned char left, right;
void main(void)
{
  displayInit();
  right = 3;
  left = 2;
  
  while (1)
  { 
    switchOff_U1();
    displayOn_U2(right);
    delay10ktcy(10);
    
    
    switchOff_U2();
    displayOn_U1(left);
    delay10ktcy(10);
    
    display(left, right);
    delay10ktcy(10);
    display(left, right);
    delay10ktcy(10);
  }
}

Blergh.

Offline

#4 2010-11-21 01:29:09

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: Microcontroller C (PIC)

And if I pass both values using pointers I get a sightly better, but not quite right output:

Left | Right
   3 |
     | 2
   3 |
     | 0
#include <pic16/pic18f4520.h>
#include <pic16/delay.h>
#include "display.h"

volatile unsigned char left, right;
void main(void)
{
  displayInit();
  right = 3;
  left = 2;
  
  while (1)
  { 
    switchOff_U1();
    displayOn_U2(right);
    delay10ktcy(10);
    
    
    switchOff_U2();
    displayOn_U1(left);
    delay10ktcy(10);
    
    display(&left, &right);
    delay10ktcy(10);
    display(&left, &right);
    delay10ktcy(10);
  }
}
void display(unsigned char *U1_value, unsigned char *U2_value)
{
  if (U1_isOn())
  {
    switchOff_U1();
    displayOn_U2(*U2_value);
  }
  else if (U2_isOn())
  {
    switchOff_U2();
    displayOn_U1(*U1_value);
  }
}

Offline

#5 2010-11-21 01:59:38

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: Microcontroller C (PIC)

Ok, so I changed the contents of display and was also playing around with the counter I had originally planned on putting in.

#include <pic16/pic18f4520.h>
#include <pic16/delay.h>
#include "display.h"

volatile unsigned char count, left, right;
void main(void)
{
  displayInit();
  count = 0;
  right = 3;
  left = 2;
  
  while (1)
  {
    //right = count % 10; // units
    //left = count / 10;  // tens
    
    switchOff_U1();
    displayOn_U2(left);
    delay10ktcy(10);
    
    
    switchOff_U2();
    displayOn_U1(right);
    delay10ktcy(10);
    
    display(&right, &left);
    delay10ktcy(10);
    display(&right, &left);
    delay10ktcy(10);
        
    count++;
  }
}
void display(unsigned char *U1_value, unsigned char *U2_value)
{
  displayOn_U1(*U1_value);
  delay10ktcy(10);
  switchOff_U1();
}

When passed by pointers, the pointer to the variable 'right' seems to instead point at the variable 'count'. So the output is:

Left | Right
   3 |
     | 2
     | 0
     | 0
   3 |
     | 2
     | 1
     | 1
   3 |
     | 2
     | 2
     | 2
   3 |
     | 2
     | 3
     | 3

Pointers are weird.

Last edited by BaconPie (2010-11-21 02:00:17)

Offline

Board footer

Powered by FluxBB