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.

Sin table in PIC....

Status
Not open for further replies.
What frequency are you expecting to output?? With 255 byte sine wave, and doing absolutely nothing else, with A 20Mhz crystal, it will yield 19Khz if you're lucky..

Don't forget there are overheads. It may not be even 19kHz for all that. It will be even lesser.

Begineer microcontroller books often mention about overheads too.
 
What frequency are you expecting to output?? With 255 byte sine wave, and doing absolutely nothing else, with A 20Mhz crystal, it will yield 19Khz if you're lucky..


I want to make inverter so, required freq from output should be 50hz but at uC it should be 100hz i think...
 
OK, working on SPWM...for 50Hz 20msec total period for half cycle i.e. 10msec according to this PR2 should be 12499..and after doing prescaler at 1:16 giving 781 so, how to store this in 8bit register?


Code:
#include <htc.h>

__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000

unsigned char sin_array[39]={128,137,146,154,163,171,179,187,194
,200, 206,212,216,220,224,227,228,229,230,229,228,227,224,220,
 216,212,206,200,194,187,179,171,163,154,146,137,128 };

void main (void) {
TRISB=0X00;
	TRISA = 0xff ;
	PEIE=1;
 	TRISC = 0 ;
	PR2 = 0b11111001 ;
	T2CON = 0b00000100 ;   
	CCP1CON = 0b00111100;                  
	PORTC = 0 ;  

while(1){
for(int i=0;i<=40;i++) {

CCPR1L = sin_array[i] ;
}
}
}
 
The PWM register is only 8-bit, which is 0-255.

You should have recalculated the PR2 (period register) so that it is less than 256. Tips: adjust the prescaler again. Use a pencil and paper and a calculator for that.

I've shown you the title of the microcontroller book earlier in the posts. It is very easy to understand and I learnt a lot from that book. The book covers almost everything basic of a microcontroller programming semester. Spend a few minutes to Google the name of the book, or to search this in your college library.

A sine wave generator powered by a microcontroller commonly use DDS algorithm, a sine wave table, a low pass filter and a DAC (R2R or PWM).
 
Last edited:
...
I'm going troll.

Please stop feeding the troll.
...

Your "Trolldar" is beeping too huh?

There are at least 4 obvious, (almost constructed?) errors in Ritesh's last code sample in post #49;
1. the sin table says 39 units, but the for() loop is seemingly aiming for 40
2. the sin table says 39 units but contains only 37 data
3. the for() loop says <=40 which will make 41 events, not matching the sin table
4. there is no time delay of any type in the for() loop

These are all really silly beginner faults that even a careful beginner would not make. I get the feeling we're being baited by someone that wants to get people running around in circles. I'm done being a Troll chef too.
 
Not troll but future fry technician.

Sadly still better with C than I am. Swordfish BASIC and 18F Asm for me. Whenever I look at C18 or the new PIC C syntax I'll get brain freezes. About once a year I'll bash my head against the wall and try my hand at it. Strangely I can read C, I just can't seem to program in it as I feel my life-force draining away when I do.

PS please stop building homemade cobbled together UPS's in India, seriously if you need one just buy one and be done with it OR run stuff on 12V instead.
 
Last edited:
I don't know what's the appeal of these inverters, but about the UPS, I can get it almost everywhere in my place. Even the computer store 1kms away from me have those for $50.

I'm comfortable with C and ASM anytime, just that I would take advantage of ASM if necessary.
 
hi guys,

Please stop abusive posts about Ritesh, some of your remarks have crossed the line, any further abusive posts will be Moderated.

Let the members who want to help an OP do so, without cluttering up a Thread with unhelpful comments.

Repeating again, whats been posted a number of times, you are under no obligation to help an OP with his project.

Eric
Moderation
 
Is that fine now?

Code:
#include <htc.h>
#include <math.h>
#include <stdio.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000

unsigned char sin_array[38]={128,137,146,154,163,171,179,187,194
,200, 206,212,216,220,224,227,228,229,230,229,228,227,224,220,
 216,212,206,200,194,187,179,171,163,154,146,137,128 };

void main (void) {
TRISB=0X00;
	TRISA = 0xff ;
	PEIE=1;
 	TRISC = 0 ;
	PR2 = 0b11111001 ;
	T2CON = 0b00000100 ;   
	CCP1CON = 0b00111100;                  
	PORTC = 0 ;  

while(1){
for(int i=0;i<=38;i++) {
__delay_ms(10);
CCPR1L = sin_array[i] ;
}
}
}
 
hi guys,

Please stop abusive posts about Ritesh, some of your remarks have crossed the line, any further abusive posts will be Moderated.

Let the members who want to help an OP do so, without cluttering up a Thread with unhelpful comments.

Repeating again, whats been posted a number of times, you are under no obligation to help an OP with his project.

Eric
Moderation

Sorry Eric, I'll now be quiet on this and other subjects.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top