Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Need help with PIC C programming.

Status
Not open for further replies.

sokalpunx

New Member
Basically I'm trying to get a very basic 2-1 MUX programmed into my PIC. This is what I have...It's not working the way it is supposed to...and I'm not sure why? Eventually this needs to be a 4-1 MUX can I get any tips or advice?

#include <p18f4520.h>
#include <delays.h>

#pragma config OSC = INTIO7
#pragma config WDT = OFF
#pragma config LVP = OFF

/*selOne = RA0, selTwo = RA1*/

int Ain, Bin, Cin, Din, selOne, selTwo;

unsigned char Dout=0;

/*Main Function*/
void main(void)

{
/*Settings*/
TRISD=0b00000000; /*Declaring all PORTD as Output port*/
TRISA=0b11111111; /*Declaring all PORTA as Input port*/
TRISB=0b11111111; /*Declaring all PORTB as Input port*/

PORTAbits.RA0=selOne;

PORTBbits.RB0=Ain;
PORTBbits.RB1=Bin;


while(1) /*Loop Forever*/
{

if(selOne==0)
{

Ain=PORTDbits.RD0;
}
else
{
Bin=PORTDbits.RD0;
}
}
}
 
Try not:

PORTAbits.RA0=selOne

Try:

Code:
#include <p18f4520.h>
#include <delays.h>

#pragma config OSC = INTIO7
#pragma config WDT = OFF
#pragma config LVP = OFF


#define selOne PORTAbits.RA0
#define selTwo PORTAbits.RA1

#define Ain PORTBbits.RB0
#define Bin PORTBbits.RB1

/*selOne = RA0, selTwo = RA1*/


unsigned char Dout=0;

/*Main Function*/
void main(void)
{
/*Settings*/
TRISD=0x00; /*Declaring all PORTD as Output port*/
TRISA=0xFF; /*Declaring all PORTA as Input port*/
TRISB=0xFF; /*Declaring all PORTB as Input port*/


while(1) /*Loop Forever*/
{
         if(selOne==0)
         {
             PORTDbits.RD0 = Ain;
         } else {
             PORTDbits.RD0 = Bin;
         }
}
}
 
Last edited:
Unless I am misunderstanding what you are trying to do, you appear to have all your assignments backward.

Shouldn't it be,
Code:
    selOne=PORTAbits.RA0;
    Ain=PORTBbits.RB0;
    Bin=PORTBbits.RB1;

    while(1) /*Loop Forever*/
    {
        if(selOne==0)
        {             
            PORTDbits.RD0=Ain;
        }
        else
        {
            PORTDbits.RD0=Bin;
        }
    }

Mike.
 
Thanks for the help

I'm really really new to C programming with the PIC. So if it looks completely wrong then I apologize >.< But yeah basically what I'm trying to program is a simple generic 2:1 MUX.

If the selOne button is 0 then whatever is at the input in A is going to be the output to D0. If the selONe button is 1 then whatever is at the input of B is going to be outputted to D0 as well.
 
You could simply have,
Code:
while(1){
    if(PORTAbits.RA0)		//if A0=1
        PORTDbits.RD0=PORTBbits.RB1;
    else
        PORTDbits.RD0=PORTBbits.RB0;
}
or, if you prefer it with braces,
Code:
while(1){
    if(PORTAbits.RA0)
    {
        PORTDbits.RD0=PORTBbits.RB1;
    }
    else
    {
        PORTDbits.RD0=PORTBbits.RB0;
    }
}

Mike.
 
Having trouble

I tried both of the codes that you guys have been kind enough to help me out with, but I'm still not getting it to work properly. I disabled MCLR to avoid having to remember to drive it high since that pin requires a 0 to do a clear. I just disabled it and turned it to an I/O port. I have it just powered now. I programmed Pommies code since its the most simple and it makes perfect sense. though I adjusted it to...this
 

Attachments

  • Hellomain.c
    557 bytes · Views: 227
This makes sense to me becuase, if PORTAbits.RA0 is 0 then RD0 is going to be whatever is RB0. And if PORTAbits.RA0 is 1 then RD0 is going to output whatever value is at RB1. This will all be digital operations correct? However, after programming it in to the PIC and testing it...nothing happens when I have RA0==0 (push button is not pressed) and I have the input "A" which is RB0 always "high." If this was to work then while RA0==0 then it should drive RD0 high as well...how do I get this to work? Is the PIC not reading the inputs?


/*This is the PIC 2:1 MUX*/

#include <p18f4520.h>
#include <delays.h>

#pragma config OSC = INTIO7
#pragma config MCLRE = OFF
#pragma config WDT = OFF
#pragma config LVP = OFF


/*Main Function*/
void main(void)
{
/*Settings*/
TRISD=0x00; /*Declaring all PORTD as Output port*/
TRISA=0xFF; /*Declaring all PORTA as Input port*/
TRISB=0xFF; /*Declaring all PORTB as Input port*/


while(1){
if(PORTAbits.RA0==0)
{
PORTDbits.RD0=PORTBbits.RB0;
}
else
{
PORTDbits.RD0=PORTBbits.RB1;
}
}
}
 
Last edited:
Need help

...any suggestions, I can't physically get this running do I have the pragma stuff set correctly? I want it to run with its internal clock.
 
4:1 Mux

OK here's a 4:1 MUX based on the codes that you guys have been nice enough to help me out now....I still can't get this to work physically... I'm disabling the mclre...external clock on the RA6...and 5vdc on pin 11 and 32 and ground 12 and 31...what am I doing wrong...or is the code faulty?


/*This is the PIC 4:1 MUX*/

#include <p18f4520.h>
#include <delays.h>

#pragma config OSC = RC
#pragma config MCLRE = OFF
#pragma config WDT = OFF
#pragma config LVP = OFF


/*Main Function*/
void main(void)
{
/*Settings*/
TRISD=0x00; /*Declaring all PORTD as Output port*/
TRISA=0xFF; /*Declaring all PORTA as Input port*/
TRISB=0xFF; /*Declaring all PORTB as Input port*/


while(1)
{
while(PORTAbits.RA0==0)
{
if(PORTAbits.RA1==0)
{
PORTDbits.RD0=PORTBbits.RB0;
}
else
{
PORTDbits.RD0=PORTBbits.RB1;
}
}
while(PORTAbits.RA0=1)
{
if(PORTAbits.RA1==0)
{
PORTDbits.RD0=PORTBbits.RB2;
}
else
{
PORTDbits.RD0=PORTBbits.RB3;
}
}
}
}
 
How about ADCON1 settings?

ADCON1 = 0x0F;

Code:
/*This is the PIC 4:1 MUX*/

#include <p18f4520.h>
#include <delays.h>

#pragma config OSC = RC
#pragma config MCLRE = OFF
#pragma config WDT = OFF
#pragma config LVP = OFF


/*Main Function*/
void main(void)
{
/*Settings*/
[b]	ADCON1 = 0x0F; //Make all digital[/b]
	TRISD=0x00; /*Declaring all PORTD as Output port*/
	TRISA=0xFF; /*Declaring all PORTA as Input port*/
	TRISB=0xFF; /*Declaring all PORTB as Input port*/


	while(1)
	{
		while(PORTAbits.RA0==0){
			if(PORTAbits.RA1==0){
				PORTDbits.RD0=PORTBbits.RB0;
			} else {
				PORTDbits.RD0=PORTBbits.RB1;
			}
		}

		while(PORTAbits.RA0=1){
			if(PORTAbits.RA1==0){
				PORTDbits.RD0=PORTBbits.RB2;
			} else {
				PORTDbits.RD0=PORTBbits.RB3;
			}
		}
	}
}

Now the code is readable.

EDIT:
ADCON1 = 0x0F;

If you are using buttons and stuff like that switches etc... remember to put a 4.7k resistor to not waste current.

also if you want to use Internal Clock :
Code:
/*This is the PIC 4:1 MUX*/

#include <p18f4520.h>
#include <delays.h>

[B]#pragma config OSC = INTIO67[/B]
#pragma config MCLRE = OFF
#pragma config WDT = OFF
#pragma config LVP = OFF


/*Main Function*/
void main(void)
{
/*Settings*/
[b]
	OSCCON = 0x72;				//8MHz clock
	OSCTUNEbits.PLLEN = 0;			//Disable PLL
	while(!OSCCONbits.IOFS);		//Wait for OSC stabilize

	ADCON1 = 0x0F;	 			//Make all digital [/b]
	TRISD=0x00; 				/*Declaring all PORTD as Output port*/
	TRISA=0xFF; 				/*Declaring all PORTA as Input port*/
	TRISB=0xFF; 				/*Declaring all PORTB as Input port*/


	while(1)
	{
		while(PORTAbits.RA0==0){
			if(PORTAbits.RA1==0){
				PORTDbits.RD0=PORTBbits.RB0;
			} else {
				PORTDbits.RD0=PORTBbits.RB1;
			}
		}

		while(PORTAbits.RA0=1){
			if(PORTAbits.RA1==0){
				PORTDbits.RD0=PORTBbits.RB2;
			} else {
				PORTDbits.RD0=PORTBbits.RB3;
			}
		}
	}
}

NEW EDIT: OSC = INTIO67
 
Last edited:
Thank you sooo much

I won't be able to check it physically til monday, but thank you for help anyways! I'll let you know if it works
 
Noticed this also:

Code:
		while(PORTAbits.RA0==0){				[b] //Change = to ==[/b]
			if(PORTAbits.RA1==0){
				LATDbits.LATD0=PORTBbits.RB0;	[b] //Change PORTDbits to LATDbits[/b]
			} else {
				LATDbits.LATD0=PORTBbits.RB1;	[b] //Change PORTDbits to LATDbits[/b]
			}
		}

		while(PORTAbits.RA0==1){
			if(PORTAbits.RA1==0){
				LATDbits.LATD0=PORTBbits.RB2;	[b] //Change PORTDbits to LATDbits[/b]
			} else {
				LATDbits.LATD0=PORTBbits.RB3;	[b] //Change PORTDbits to LATDbits[/b]
			}
		}

Besides that seems to work ok. I used two(2) Buttons 1 DIP and 1 LED to test it.
 
Last edited:
Need help again.

^_^ alright well

------------------------------------------------------------------------

/*This is the PIC 4:1 MUX*/

#include <p18f4520.h>
#include <delays.h>

#pragma config OSC = INTIO67
#pragma config MCLRE = OFF
#pragma config WDT = OFF
#pragma config LVP = OFF


/*Main Function*/
void main(void)
{
/*Settings*/

OSCCON = 0x72; //8MHz clock
OSCTUNEbits.PLLEN = 0; //Disable PLL
while(!OSCCONbits.IOFS); //Wait for OSC stabilize

ADCON1 = 0x0F; //Make all digital
TRISD=0x00; /*Declaring all PORTD as Output port*/
TRISA=0xFF; /*Declaring all PORTA as Input port*/
TRISB=0xFF; /*Declaring all PORTB as Input port*/


while(1)
{
while
{

while(PORTAbits.RA0==0) //Change = to ==
{
if(PORTAbits.RA1==0)
{
LATDbits.LATD0=PORTBbits.RB0; //Change PORTDbits to LATDbits
}
else
{
LATDbits.LATD0=PORTBbits.RB1; //Change PORTDbits to LATDbits
}
}

while(PORTAbits.RA0==1){
if(PORTAbits.RA1==0){
LATDbits.LATD0=PORTBbits.RB2; //Change PORTDbits to LATDbits
}
else
{
LATDbits.LATD0=PORTBbits.RB3; //Change PORTDbits to LATDbits
}
}
}
}


--------------------------------------------------------------------------

Here's the code that I ended up using...


What I need help with is how to get this code to switch between the "channels" automatically. Like instead of manually pressing the switches, it would, automatically switch channels every clock cycle... I.E 00, 01, 10, 11 and then start back at 00 and continually do it infinitely... any suggestions or help?
 
add a switch/if statement and a counter is all i can say

But like:
Code:
/*This is the PIC 4:1 MUX*/

#include <p18f4520.h>
#include <delays.h>

#pragma config OSC = INTIO67
#pragma config MCLRE = OFF
#pragma config WDT = OFF
#pragma config LVP = OFF


/*Main Function*/
void main(void)
{
[B]/*Variables*/
char count = 1;[/B]
/*Settings*/

OSCCON = 0x72; //8MHz clock
OSCTUNEbits.PLLEN = 0; //Disable PLL
while(!OSCCONbits.IOFS); //Wait for OSC stabilize

ADCON1 = 0x0F; //Make all digital
TRISD=0x00; /*Declaring all PORTD as Output port*/
TRISA=0xFF; /*Declaring all PORTA as Input port*/
TRISB=0xFF; /*Declaring all PORTB as Input port*/


while(1){
[B]    switch(count)
    {
        case 1:
            LATDbits.LATD0=PORTBbits.RB0;
        case 2:
            LATDbits.LATD0=PORTBbits.RB1;
        case 3:
            LATDbits.LATD0=PORTBbits.RB2;
        case 4:
            LATDbits.LATD0=PORTBbits.RB3;
    }

    if(count == 4 )
        count = 1;
    else
        count++;
[/B]
    while(PORTAbits.RA0==0){ //Change = to ==
        if(PORTAbits.RA1==0)
            LATDbits.LATD0=PORTBbits.RB0; //Change PORTDbits to LATDbits
        else 
            LATDbits.LATD0=PORTBbits.RB1; //Change PORTDbits to LATDbits
    }

    while(PORTAbits.RA0==1){
        if(PORTAbits.RA1==0)
            LATDbits.LATD0=PORTBbits.RB2; //Change PORTDbits to LATDbits
        else
            LATDbits.LATD0=PORTBbits.RB3; //Change PORTDbits to LATDbits
    }
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top