|
|
View previous topic :: View next topic |
Author |
Message |
acm45
Joined: 29 Dec 2008 Posts: 18 Location: Italy
|
16F88 PWM-serial compatibility |
Posted: Mon Dec 29, 2008 4:23 am |
|
|
Hi!
I want to control the duty cycle of the pwm by pc with the serial port, I woud like to use the TX, RX pin of portB (pin B5, B2).
I write this code:
Code: |
#include <16F88.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPUT //No Power Up Timer
#FUSES MCLR //Master Clear pin enabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOIESO //Internal External Switch Over mode disabled
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8)
#include <stdlib.h>
#build (reset=0x1,interrupt=0x4) \\ for bloader
#ORG 0x0F00,0x0FFF {}
char string[6];
unsigned int duty_cycle = 0;
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_4,249,1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(125);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
// TODO: USER CODE!!
printf("Ready...\n\r");
while(1)
{
while(!kbhit());
gets(string);
printf("Received: %s\n\r",string);
duty_cycle = atoi(string);
set_pwm1_duty(duty_cycle);
}
}
|
The pwm output is on Pin B0, the frequency and the initial duty cycle is ok!
I receive the "ready" string but the pic don't receive the values I send.
If I modify the serial pins like:
#use rs232(baud=9600,parity=N,xmit=PIN_A3,rcv=PIN_A2,bits=8)
it functions!
Why I can't use the B3,B5 pins as serial port? |
|
|
andyfraser
Joined: 04 May 2004 Posts: 47 Location: UK
|
|
Posted: Mon Dec 29, 2008 4:36 am |
|
|
From your code, it looks like you are using a bootloader.
Could it be that the bootloader is messing with the configuration of the hardware serial port on RB2 and RB5 ?
Try your code without the bootloader and see if that makes a difference.
Andy |
|
|
acm45
Joined: 29 Dec 2008 Posts: 18 Location: Italy
|
|
Posted: Mon Dec 29, 2008 5:10 am |
|
|
Thank for the answer Andy but, I've just tried without the bootloader and doesn't function! |
|
|
Ttelmah Guest
|
|
Posted: Mon Dec 29, 2008 5:35 am |
|
|
Remember you will need to change the build instruction to run without the bootloader.
Prove your serial hardware is working. You know the output side is OK, so write a simple program to echo the characters back, and see if you are actually receiving.
I really would suspect the bootloader.
As general 'comments', beware of your string length. You allow six characters for the incoming data, but 'gets', adds a terminating '\0', and most terminal programs, send CR+LF, so it'd not take much typed, to overflow the buffer. I'd set the buffer a little larger (perhaps 10 characters), and use the GET_STRING function from input.c, with a length limit added as being a lot safer.
You really need to use 'atol', not 'atoi'.
Best Wishes |
|
|
acm45
Joined: 29 Dec 2008 Posts: 18 Location: Italy
|
|
Posted: Mon Dec 29, 2008 6:55 am |
|
|
Thank you for the hint Ttelmah, I tested the serial hardware without the bootloader with a simple echo program.
It functions corectly only if I use pins different from B5, B2!
If I use B5 and B2, I can only receive data from pic!
The bootloader I use is the bloader (sparkfun), and needs B5,B2 as TX/RX, his test program (serial echo) functions correctly!
What can be wrong? |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Dec 29, 2008 7:26 am |
|
|
Remove this line, it's configuring RB2 as SDO.
Code: | setup_spi(SPI_SS_DISABLED); |
|
|
|
acm45
Joined: 29 Dec 2008 Posts: 18 Location: Italy
|
|
Posted: Mon Dec 29, 2008 8:48 am |
|
|
FvM you find the problem!!!
I removed the line and now all is ok!!!
Also with the bootloader, all ok!
Thank you very much!!!
|
|
|
|
|
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
|