Jump to content
Sign in to follow this  
mmckban

What's Wrong With This Code?

Recommended Posts

Howdy,

 

I'm trying to learn pic programming and have made a little progress, but am stumped right now. I'm using a BoostC v. 6.60 and a PIC16F818 and trying ultimately to get it to read a key matrix and put a binary code out on 4 lines indicating which key was pressed. I couldn't get that to work so right now the code below just reads one button press on portb.3 and lights LED's based on the number I set mkey equal to.

 

First a small description of the circuit. portb.3 has a 10K resistor for a pullup and is connected to one leg of a pushbutton. The other leg goes to ground. PortA has five LED's connected to various pins, and they are connected to +5 through resistors. So to turn on an LED you pull the pin low.

 

What I think it should do is leave the shift LED on until you press the button, then turn it off for 250ms, turn it back on and turn some of the other LEDs on for 500ms. What actually happens is the shift LED lights when I power up the circuit, then goes out when I push the button. If I hold the button down, after a second or so the shift led starts blinking at what looks to be about 500ms intervals, and the correct code LEDs light. This continues until I let up the button upon which all LEDs stay off. If I press the button and hold it for less than a second nothing happens at all.

 

Now for the code:

 

#include <system.h>

//Target PIC16F818 configuration word
#pragma DATA _CONFIG, _INTRC_IO & _PWRTE_OFF & _WDT_OFF & _CP_OFF
//Set clock frequency
#pragma CLOCK_FREQ		8000000


#define SHIFT_BIT	6
#define CODE_BIT_0	7
#define CODE_BIT_1	0
#define CODE_BIT_2	1
#define CODE_BIT_3	2
#define     NOKEY           0xFF

char porta_state = 0xFF;
bool shifted = false;

void main()
{
char mkey = NOKEY;

set_bit(osccon,IRCF2); // set internal oscillator to 8mhz
set_bit(osccon,IRCF1); // "
set_bit(osccon,IRCF0); // "

//Configure ports.  0 = output, 1 = input
trisa = 0b00000000;
trisb = 0b00001111;

//Initialize ports
porta = 0xFF;
portb = 0x00;

//option_reg.NOT_RBPU = 0;  // turn on weak pull-ups on portb
ccp1con = 0b00000000;	//disable comparators
adcon0.ADON = 0;		// turn off analog to digial module
sspcon.SSPEN = 0;		// turn off SSP module
intcon = 0x00;			// turn off interupts

//Endless loop
while( 1 )
{
	mkey = NOKEY;
	if (portb.3 == 0)
	{
		delay_ms(5);
		mkey = 0x05;
	}

	if (mkey != NOKEY)
	{
		porta_state.SHIFT_BIT = 1;
		porta = porta_state;
		delay_ms(250);
		porta_state.SHIFT_BIT = 0;

		porta_state.CODE_BIT_0 = mkey.0;
		porta_state.CODE_BIT_1 = mkey.1;
		porta_state.CODE_BIT_2 = mkey.2;
		porta_state.CODE_BIT_3 = mkey.3;

		porta = porta_state;
		delay_ms(500);
		porta = 0xFF;
	}
	else
	{
		porta_state.SHIFT_BIT = 0;
		porta = porta_state;
	}
}
}

 

What am I doing wrong?

 

Thanks,

Moses

Share this post


Link to post
Share on other sites

I figured it out! I had to disable Low Voltage Programming (& _LVP_OFF in the #pragma DATA _CONFIG) because it took over pin B3. Guess I needed to read the datasheet more carefully before I posted.

 

Does anyone know of a good PIC tutorial that uses C instead of assembly? I know C and C++ but don't have the time or much inclination to learn assembly.

 

Thanks,

Moses

Share this post


Link to post
Share on other sites
I figured it out!  I had to disable Low Voltage Programming (& _LVP_OFF in the #pragma DATA _CONFIG) because it took over pin B3.  Guess I needed to read the datasheet more carefully before I posted.

 

Does anyone know of a good PIC tutorial that uses C instead of assembly?  I know C and C++ but don't have the time or much inclination to learn assembly.

 

Thanks,

Moses

 

 

There are many good BoostC Examples right on the SourceBoost website.

 

MicrochipC has some decent examples altho most are witten for MCC18 or HI-Tech C, but should give you a few examples.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...