Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! 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

Advanced Member level 2
Joined
Jun 26, 2006
Messages
503
Helped
44
Reputation
88
Reaction score
15
Trophy points
1,298
Location
VddVss
Activity points
3,882
openusart pic18

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:
#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 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 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 :-(

Help me :-([/code]
 

pic18f2550 serial openusart

Well, I must admit I’ve never done PIC before, but a quick inspection of your code makes me have doubts about your calling of OpenUSART and OpenTimer2 functions. Unless you’ve chosen a strange negative logic to define your flags, in my experience I found that the flags are usually OR-ed (or added) and not AND-ed together.
I would think your calls should be:
Code:
      OpenUSART( USART_TX_INT_OFF | 
      USART_RX_INT_OFF | 
      USART_ASYNCH_MODE | 
      USART_EIGHT_BIT | 
      USART_CONT_RX | 
      USART_BRGH_HIGH, 
      129 );
and
Code:
      OpenTimer2(TIMER_INT_OFF | T2_PS_1_1 | T2_POST_1_1);
(note the vertical bar as in "bitwise OR"; you might use "+" as well).

Does this ring a bell?
Arthur
 

openusart 9600

Well, we must never forget that:

(A | B) = !(A & B)

Which notation you use depends on how you defined your flags.

It might be that HiTech, in order to make the language more "natural sounding" so that the programmer can read out "I want the UART to have USART_TX_INT_OFF and USART_RX_INT_OFF.. etc" have defined flags in an inverted convention than the normals ones used in the industry.

0010
|
1000
----
1010

1101
&
0111
---
0101 = !(1010)

As you can see, both are the same and do not have any impact at runtime.

Plus, this portion is stolen from the documentation, so it should not be wrong ;-)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top