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.
bananasiong said:
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.

yup, I use C 99.9% of time but I was thinking more in course of
Code:
...
while(1){
...
  asm('rrf PORTD,1');
...
}
saves the ram for that extra variable you would use to rotate/shift and then assign to port..
 
bananasiong said:
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.

Thanks!! I know know how it works, wanna try some. Thanks!!
 
The first program of mine!!! With helps from u guys! Thank u so much!!

I need to change to PIC18F4580 because my PIC18F4520 is worn out. Hmm...

Code:
/*This program tells a series of LEDs in PORTD to flash (by pressing Push Button RA4) one by one in forward direction, from RD0 to RD7.*/
/*When it reaches RD7, backward direction executed from RD7 to RD0 again. When PB RA4 is pressed again, the whole system stop. */

#include <p18f4580.h>
#include <delays.h>

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

int i, iKey, iPrevious;

unsigned char cShift=1; /*Declaring shift bit interval*/
unsigned char cPBState=1; /*State of the Push Button*/

void vFlashing(void); /*Declaring flashing mode1*/

/*Flashing Mode*/
void vFlashing(void)

{
	if (cShift==15) /*Count cShift to 15x*/
	{
		cShift=1; /*Restarting cShift value*/
	}

	if (cShift<8) /*When cShift smaller than 8*/
	{
		LATD=0b00000001<<cShift; /*Forward flash*/
		Delay10KTCYx(50); /*Delays*/
		cShift++; /*Increment cShift*/
	}
	else /*When cShift bigger than 8*/
	{
		LATD=0b10000000>>(cShift-7); /*Backward flash*/
		Delay10KTCYx(50); /*Delays*/
		cShift++; /*Increment cShift*/
	}
}

/*Main Function*/
void main(void)

{	
	/*Settings*/
	TRISD=0b00000000; /*Declaring all PORTD as an Output Port*/
	TRISA=0b00010000; /*Declaring RA4 as an Input Port*/
	
	/*Initializing*/
	iKey=0;
	cPBState=1;

	while(1) /*Loop Forever*/
	{
		iPrevious=iKey; /*Copy the last value*/
		iKey=PORTAbits.RA4; /*Assigning new value*/

		/*Push Button RA4 Function ON/OFF Button*/
		if(iKey==0 && iPrevious==1) /*Getting information*/
		{
			if(cPBState==0) /*Changing the PBState*/
			{
				cPBState=1;
			}
			else
			{
				cPBState=0;
			}
		}

			switch (cPBState) /*Switch function for cases*/
			{	
				case 0:
				vFlashing(); /*If case 0, do flashing1*/
				break;
				
				case 1: /*If case 1, do nothing*/
				break;
				
				default:
				break;
			}
	}
}

Okay, I want to ask a question now:

If I want this thing on a small pcb (not on a demo board), is it possible to make PCB layout according to the data sheet of this MC and make all the Vcc and GND and all of their routes, making PORTD as an output, say I connect them with connectors so I can drive several LEDs??

Is that how these things work??? Is that how ppl out there makin MCU as "brain" of their PCB or system, I would say. ??
 
Help is needed. Thanks :)

Hey sorry i'm new here. Just to ask how do i make the LED blink(using C program statements) Light up a combination of LEDs on the target board(using C program statements) Light up a LED when a switch is pressed (using C program statements). I'm a student and i'm doing some research on it. But i can't get to find any help regarding the Question above. Can anyone help me out? Thanks lots:)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top