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.

help on my led code

Status
Not open for further replies.

xundeadx

New Member
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.
 
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.
 
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.
 
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?
 
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:
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
 
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;
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top