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.

Buggy/bad code to do serial Tx and PWM

Status
Not open for further replies.

vsmGuy

New Member
Hi,

I wanted to send out data at 9600 bps, and do variable duty cycle PWM @40Khz.

My PIC18F2550 is clocked @ 20Mhz and here is the code so far:

Code in PICC:

Code:

[/code]
#define _PLIB

#include <htc.h>
// will automatically include peripheral library header files when --runtime=+plib is used (had to pass this explicitly to the commandline)

#include <pic18.h>
#include <stdio.h>

void
main(void)
{
GIE=0; /* no interrupts are used */
IPEN=0;

TRISC=0; /* PORTC.1 is the output from PWM */

OpenUSART( USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH,
129 );

/*
This function configures the specified PWM channel for period and for time base.
PWM uses only Timer2.
In addition to opening the PWM, Timer2 must also be opened with an OpenTimer2() statement before the PWM will operate.

period Can be any value from 0x00 to 0xff.
This value determines the PWM frequency by using the following formula:
PWM period = [(period) + 1] x 4 x TOSC x TMR2 prescaler */

OpenTimer2(TIMER_INT_OFF & T2_PS_1_1 & T2_POST_1_1);

OpenPWM2(124);
SetDCPWM2(0);

//SetOutputPWM2(SINGLE_OUT, PWM_MODE_1);

while (1){
putsUSART("Test\r\n");
}

CloseTimer2();

ClosePWM2();
CloseUSART();
}
[/code]

Code is incorrect as:

1. The first character (first "T") never makes it, but then I seem to get the remaining ones

2. I get no PWM output and for the love of --- could not figure out how to use SetDCPWM2, and just now, in times of desperation I put in random values, to get no change/no output :-(

Help me :-(
 
When in doubt, do it in assmebly :D Altohugh for larger programs, I just use mikroC (the free version) then hack the generated assembly code. Did you find out why the high-tech generated code didnt' perform as required?
 
Nah . wasting time i doing something that can be done in a easier manner makes more sense to me. Just like writing in C than ASM ;-)

I am willing to write in ASM if someone pays me to OR for Very tight code. Never for hobby purpose.
 
Ok, no more programming language war.

I'm not really into PICC, but from my understanding,
OpenPWM2(124);
this is the PWM period (or setting up PR2 value)

and
SetDCPWM2(0);
this is "set duty cycle PWM?"

So you're having 0 duty cycle, what are you expecting at the output?
 
As I said in the first post : I get no PWM output and for the love of God could not figure out how to use SetDCPWM2, and just now, in times of desperation I put in random values, to get no change/no output :-(
 
Hey, no worries. I am just grumpy about HiTech - wasted a LOT of time behind it because I yhought "why not try it out because MCP acquired it".

Thanks bananasiong for your time! I appreciate it :)

This is one of those things that was not meant to come out well, that's all. Not eveything works out fine in life.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top