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.

New using PIC18F4520 with C Programming

Status
Not open for further replies.
The way that the key read works is by keeping a copy of how the key was 10mS ago. If it was not pressed previously and it is now pressed then we have a new keypress. Next time around the previous value is pressed and the new value is pressed and so it isn't a new keypress.

Did you try all the code. I haven't tried it but I think it should work.

Mike.
 
it works! indeed!! I really thank u Pommie. Let see on Monday how could I learn more about this LED driving with MCU. Good weekend for u guys!!
 
Pommie said:
Have you extended it to flash all the LEDs? Do you now understand it?

Mike.


Yes, Pommie. Now I have my flashing modes ON and OFF by pressing the SAME Push Button.

Another thing is "Why my MC is now cannot programmable!?!" I clicked program several times but it wouldn't PROGRAM.
 
mrfunkyjay said:
Yes, Pommie. Now I have my flashing modes ON and OFF by pressing the SAME Push Button.

Another thing is "Why my MC is now cannot programmable!?!" I clicked program several times but it wouldn't PROGRAM.

Need more info, what sort of programmer?
 
I am using MPLab ICD2. I am using P18F4520 MC. Is this device already damaged or what? Care to help? Thanks!

Btw, what do I mean by cannot be programmable is:

I have my last code inside, but now I want to overwrite it, it appears to remember always the last code I had programmed.
 
The ICD2 will erase before programming. Is it erasing, there are diagnostics in the programming tab.
PICs can withstand thousands of reprogrammings, not likely you've worn it out.
Have you tried another PIC, does the ICD2 program it?
 
Hmm, when I received this kit, my colleagues had worked with it several months, even 1 year maybe? So worn out thingy seems normal. Me myself I had reprogrammed it around 80-100 times. Hmm, anything else to check. Now I can't find any MC around this office, ppl are not here yet (Software).
 
Okay guys, a new problem has arrived and I need your comments regarding this!! :D :D

I want to simplify my Flashing command. As you can see I made it by assigning LATD and delay manually.

Now I want to make this using BIT SHIFTING method. I have tried this code:

Code:
main()
  {
    unsigned int Value=4;          /*  4 = 0000 0100 */  
    unsigned int Shift=2;

    Value = Value << Shift;        /* 16 = 0001 0000 */  

    Value <<= Shift;               /* 64 = 0100 0000 */  

    printf("%d\n", Value);         /* Prints 64      */  
  }

But I am not sure, because all I want to have is:

Code:
/*PORTD*/
0000 0000 /*Initial condition*/
0000 0001 /*RD0 turned on*/
|
|
|
|
1000 0000 /*RD7 turned on*/

and then looping again to Initial Condition

This thing runs the same way like previous. So, care to give comments? Thanks!!!
 
Hi,
Try this
Code:
char shift, value = 1;

LATD = value << shift;
shift ++;
if (shift == 8)
{
    shift = 0;
}
You need to add delay to make it visible. Or put it in the timer ISR is simpler.
 
Last edited:
Right shift instead of left shift, >>
Also, you can preset bit 7 of value.

If you use the whole PORTD as output, you can just shift the port without using any variable.
 
how about the cShift++ increment?

My logic says, I should make another cValue, (cValue2 for example). This is considered 1000 0000 or equal to =128. Then, shifting the bit backwards from 7 to 0 with the same cShift or shift interval of 1 bit.

If cShift reaches 8, the cValue2 now begins to execute with right shift.

But another problem is coming up, check whether I made mistake somewhere, thanks!

Code:
/*Flashing Mode*/
void vFlashing(void)

{
	LATD=cValue1<<cShift; /*Forward flash*/
	Delay10KTCYx(20); /*Delays*/
	cShift++; /*Increment cShift up to..*/

	if (cShift==8) /*When cShift reaches 8*/
	{
		LATD=cValue2>>cShift; /*Reverse flash*/
		Delay10KTCYx(20); /*Delays*/
		cShift++;
	}
}
 
Hi,
That won't work because the Reverse flash part is executed only when cShift is 8, with cShift++, you won't get into it anymore.

Do you need the LED to light go and back and go and back.... ? Try this:
Code:
char shift;

if (shift == 15)
{
	shift = 0;
}
if (shift < 8)
{
	LATD = 0x01 << shift;
}
else
{
	LATD = 0x80 >> (shift - 7);
}
shift ++;
and you'll add the delay.
 
Bananasiong,

Nice bit of code. However, shouldn't it be if(shift==16) and (shift-8).

Mike.
 
My code gives the sequence like this (in term of bit in PORTD):
0 1 2 3 4 5 6 7 6 5 4 3 2 1 0 and over and over.

If if(shift==16) and (shift-8), it will be
0 1 2 3 4 5 6 7 7 6 5 4 3 2 1 0 and over and over.

I think for better illusion for the bouncing, it should be like this:
Code:
char shift;

if (shift == 15)
{
	shift = [B]1[/B];
}
if (shift < 8)
{
	LATD = 0x01 << shift;
}
else
{
	LATD = 0x80 >> (shift - 7);
}
shift ++;
which gives the sequence of
0 1 2 3 4 5 6 7 6 5 4 3 2 1 0 1 2 3 4 5 6 .......
 
Very nice, I will try this tommorow, hmm... I would like to know the 0x00 thingie here.

I saw 0x01 and 0x08. What is this represents? and the shift - 7. Care to explain how could you come out with that?

one more question will be: what is that shift == 15??? Thanks!
 
Yep. 0x01 is also 0b00000001. Since shift is incrementing, by doing 0x01 << shift, 0x01 will be left shifted according to the number in shift.

It is the same for right shifting 0x80, which is 0b10000000. shift - 7 determines how many times 0x80 get right shifted.

if (shift == 15) <-- this means if shift equals to 15.

Basically shift is counting from 1 to 15, and the LED sequence is determined by shift.
 
Yes, there're instructions for rotating the register in asm. But this is the discussion about C.

For PIC16F, rrf and rlf.
For PIC18F, rrcf, rrncf, rlcf, rlncf which have taken care of the carry bit.
 
Status
Not open for further replies.

Latest threads

Back
Top