|
|
View previous topic :: View next topic |
Author |
Message |
kel
Joined: 17 Oct 2005 Posts: 68 Location: Brisbane
|
16f628a not working! |
Posted: Thu Dec 15, 2005 10:05 pm |
|
|
Guys i need your help.Im trying to send data to the PC and blink the led once data.I'm using pic16f628A with internal UART. I run the it with an external clock frequency 20MhZ.Nothing works, not even the led.
I've tried to simulate the code on MPLAB7.2,it works but doesn't work on the chip.Please help me out!
Here is the code....
Code: |
#include <16F628A.h>
#device *=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES MCLR //Master Clear pin enabled
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOCPD //No EE protection
#use delay(clock=20000000)
//#define RTC_RST PIN_A4
//#define RTC_SCLK PIN_A5
//#define RTC_IO PIN_A6
#use rs232(baud=9600,parity=N,xmit=PIN_B1,rcv=PIN_B2,bits=9)
#define led PIN_A4
void delay_second(unsigned int sec); //function to create delays in seconds
void blink_led(char ntimes); //number of times to blink the led
unsigned char counter=0;
unsigned char timer;
#int_TIMER0
TIMER0_isr()
{
set_timer0(100); /*set timer0 for interrupt to occur evry ms*/
counter++;
//blink_led(5);
}
void main()
{
unsigned int buff;
timer=0;
set_tris_a(0x04); /*set portA pins to output (00000000)*/
printf("program initializing, please wait...\n");
delay_second(2);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
/* rtc_init();
adc_init();
lcd_init();
kbd_init();*/
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
/*setup_oscillator(False);*/
while(1)
{
buff=((unsigned int)counter-'0');
for(buff=100000; buff>0; buff/=10)
printf("value is:= %d\n",(buff%10)); /*printing a BCD number*/
blink_led(5); /*when done blink the led 5 times*/
}
}
void delay_second(unsigned int sec)
{
while(sec--)
delay_ms(1000);
}
void blink_led(char ntimes)
{
unsigned char s; /*counter*/
unsigned int n;
for(s=0; s<ntimes; s++)
{
output_high(led); /*turn on the led*/
for(n=0; n<6550; n++); /*delay*/
output_low(led); /*turn off the led*/
for(n=0; n<6550; n++);
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 15, 2005 10:21 pm |
|
|
There are quite a few things wrong:
Change it to NOLVP, unless you're using a Low Voltage programmer.
Quote: | #use rs232(baud=9600,parity=N,xmit=PIN_B1,rcv=PIN_B2,bits=9) |
Change it so xmit is on pin B2, and rcv is on pin B1. Then you can
will be using the hardware UART. Also change it to 8 bits.
Quote: | #define led PIN_A4 |
RA4 is an open drain output. It can't put out a positive voltage.
It can only put out a 0 volt level. Does your LED circuit expect
a positive voltage from pin A4 ?
Quote: | #int_TIMER0
TIMER0_isr()
{
set_timer0(100); /*set timer0 for interrupt to occur evry ms*/
counter++;
//blink_led(5);
} |
Don't do long, time-consuming things in an isr. Leave the blink_led()
function commented out. In fact, delete that line.
Quote: |
unsigned int buff;
for(buff=100000; buff>0; buff/=10) |
'buff' is declared as a 8-bit unsigned variable. An 'int' is 8 bits in CCS.
You need a 32-bit variable to hold a value of 100K. Declare 'buff'
as an 'int32' instead.
Quote: | void blink_led(char ntimes)
{
unsigned char s; /*counter*/
unsigned int n;
for(s=0; s<ntimes; s++)
{
output_high(led); /*turn on the led*/
for(n=0; n<6550; n++); /*delay*/
output_low(led); /*turn off the led*/
for(n=0; n<6550; n++);
}
} |
Why use for(;;) loops for a software delay ? CCS provides delay_us()
and delay_ms() functions. Use them. |
|
|
Skel Guest
|
Thank programmer! |
Posted: Thu Dec 15, 2005 10:34 pm |
|
|
Thanks alot Programmer, infact I will try this out..
cheers! |
|
|
Ttelmah Guest
|
|
Posted: Fri Dec 16, 2005 6:14 am |
|
|
As a minor comment, also change:
#FUSES HS
to
#FUSES EC
Tis will probably not cause a problem on the 628, but 'HS', selects the higher gain internal amplifier neded for a crystal oscillator. On some chips, overdriving this ocillator, can cause problems. EC, is for when you have a 'external clock', which is what you say you have.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|