Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 3rd July 2009, 04:32 AM   #1
Default help on my led code

Code:
#include <p18f4520.h>
#include <delays.h>
#include <stdio.h>
//#include <xlcd.h>
#include <pwm.h>
#include <timers.h>
#pragma config WDT = OFF	// off watch dog timer
#pragma config LVP = OFF   // off low voltage programing
#pragma config OSC = EC  //config osc = highspeed

void main (void)
{
	unsigned char result; 
	ADCON0 = ADCON0 ^ 0x01; //turn on ADC
//	TRISA = 0b00000001;
	TRISB = 0; //set port b as output
	PORTB = 0; // clear port b
	TRISC = 0; //set port c as output
	OpenTimer2(TIMER_INT_ON & T2_PS_1_16 & T2_POST_1_1); // set timer on prescale 1:1, postscale 1:1
	//PR2=64;
	OpenPWM1(0xff); // turn own PWM
	
	while(1)
	{
		ADCON0= ADCON0 ^0x02; //adc conversion
	//	Delay1KTCYx(1);
		while(ADCON0 & 0x02){}// loop till convension end
	//	Delay1KTCYx(1);
		result = ADRESH; //store adc result
		setDCPWM1(result);
		PORTB = PORTCbits.RC2;
		Delay1KTCYx(100);
	}
}
hello i am a newbie and above is my code for my pwm blinking program. i am using a p18f4520 ic chip and i am using mplab c18 complier. i got 2 problems 1 is PORTB=PORTCbits.RC2 and setDCPWM1(result)

portb=portcbit.rc2 cant let me set all port b = to rc2... it wont blink all my LED on my picdem 2 plus board

and the setDCPWM1(result) got error on my build.
xundeadx is offline  
Old 3rd July 2009, 04:36 AM   #2
Default

hi for the error of my setDCPWM1(result)

Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\Documents and Settings\amkuser\Desktop\sample projects c18\testing pwm1.o".
Clean: Done.
Executing: "C:\MCC18\bin\mcc18.exe" -p=18F4520 /i"C:\MCC18\h" "testing pwm1.c" -fo="testing pwm1.o" -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
C:\Documents and Settings\amkuser\Desktop\sample projects c18\testing pwm1.c:30:Warning [2058] call of function without prototype
Executing: "C:\MCC18\bin\mplink.exe" /p18F4520 /l"C:\MCC18\lib" /k"C:\MCC18\bin\LKR" "testing pwm1.o" /u_CRUNTIME /z__MPLAB_BUILD=1 /m"trial 1.map" /w /o"trial 1.cof"
MPLINK 4.31, Linker
Copyright (c) 2009 Microchip Technology Inc.
Error - could not find definition of symbol 'setDCPWM1' in file './testing pwm1.o'.
Errors : 1

Link step failed.
xundeadx is offline  
Old 3rd July 2009, 05:40 AM   #3
Default

You can't set a byte equal to a bit. You could do

PORTB=(PORTCbits.RC2==0)?0:0xff;

However you should be using LATB instead of PORTB.

As for the other error, the function is spelt wrong. It should be,
SetDCPWM1(result);

Mike.
Pommie is online now  
Old 3rd July 2009, 06:23 AM   #4
Default

so lets say i got 4 led @ portb 0-3 how shd i do it?
xundeadx is offline  
Old 3rd July 2009, 06:32 AM   #5
Default

oo! thx for the setDC thing. cuz i read my header it is setDC haha.

i understand now that portcbit = bit and portb = byte.. so how can i let my 4 led which is at portb0 - 3 light up at the same time?
xundeadx is offline  
Old 3rd July 2009, 06:40 AM   #6
Default

There are various way,

The simplest is,
LATB=0x0f;
but this resets the top bits

To keep the other bits the same use,
LATB=(LATB & 0xf0) | 0b1111;
and change the 1s to 0s for the LEDs you don't want lit.

So, to set them depending on RC2 you could do,
Code:
if(PORTCbits.RC2==0)
    LATB=(LATB & 0xf0);
else
    LATB=(LATB & 0xf0) | 0b1111;
Mike.

Last edited by Pommie; 3rd July 2009 at 06:41 AM.
Pommie is online now  
Old 3rd July 2009, 06:42 AM   #7
Default

sweet i will try it out.. but i did something on my own


SetDCPWM1(result);
PORTBbits.RB0 = PORTCbits.RC2;
PORTBbits.RB1 = PORTCbits.RC2;
PORTBbits.RB2 = PORTCbits.RC2;
PORTBbits.RB3 = PORTCbits.RC2;
i did something like that but the blinking no good it blink one at a time
xundeadx is offline  
Old 3rd July 2009, 10:08 AM   #8
Default

hi again.. i edited the code like u show me and now it dim the light by blinking fast or slow but i want to use PWM to control the frequency to blink without using delay. any idea how can it be done? i tried to change the pre and post scaler it wont do any thing. i also tried the OpenPWM(0x00)<-- to my understanding this is the frequency/period changing also called PR2 i think...but both usless...

Code:
#include <p18f4520.h>
#include <delays.h>
#include <stdio.h>
//#include <xlcd.h>
#include <pwm.h>
#include <timers.h>
#pragma config WDT = OFF	// off watch dog timer
#pragma config LVP = OFF   // off low voltage programing
#pragma config OSC = EC  //config osc = highspeed

void main (void)
{
	unsigned char result; 
	ADCON0 = ADCON0 ^ 0x01; //turn on ADC
//	TRISA = 0b00000001;
	TRISB = 0; //set port b as output
	PORTB = 0; // clear port b
	TRISC = 0; //set port c as output
	OpenTimer2(TIMER_INT_ON & T2_PS_1_16 & T2_POST_1_16); // set timer on prescale 1:1, postscale 1:1
	//PR2=64;
	OpenPWM1(0xff); // turn own PWM
	
	while(1)
	{
		ADCON0= ADCON0 ^0x02; //adc conversion
	//	Delay1KTCYx(1);
		while(ADCON0 & 0x02){}// loop till convension end
	//	Delay1KTCYx(1);
		result = ADRESH; //store adc result
		SetDCPWM1(result);
		if(PORTCbits.RC2==0)
    	LATB=(LATB & 0xf0);
		else
    	LATB=(LATB & 0xf0) | 0b1111;
xundeadx is offline  
Reply

Tags
code, led

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
16F84a code to 16F628a code JKF1000 Micro Controllers 18 17th October 2009 06:06 PM
Need assembly code or other source code of a "Miniature digital pin number lock"... chunei Micro Controllers 1 13th August 2007 06:45 PM
First Code Ever... striker2509 Micro Controllers 4 3rd June 2005 04:36 PM
Help with pic code hantto Micro Controllers 5 23rd August 2004 01:10 PM
some testing code.... of code of 89C51 for LCD kumar_3k Electronic Projects Design/Ideas/Reviews 0 5th November 2003 06:23 PM



All times are GMT. The time now is 06:41 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker