jean12
Advanced Member level 2
- Joined
- Aug 27, 2013
- Messages
- 529
- Helped
- 5
- Reputation
- 12
- Reaction score
- 6
- Trophy points
- 18
- Activity points
- 5,497
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 sbit UP at RB1_bit; sbit DOWN at RB2_bit; unsigned char sine_table[]={123,127,131,133,137, 141, 145, 148, 150, 154, 158, 162, 165, 167,171, 174,177,181,182,186,189,192,195,197,200,203,205,208,209,212,214,216,218,219,222,223,225,227,227,229,230,231,232,233,233,234,235, 235,235,236,236,236,235,235,235,234,233,233,232,231,230,229,227,227,225,223,222,219,218,216,214,212,209,208,205,203,200,197,195,192,189,186,182,181,177,174,171,167,165,162,158,154,150,148,145,141,137,133,131,127,123,119,115,113,109,105,101,98,96,92,88,84,81,79,75,72,69,65,64,60,57,54,51,49,46,43,41,38,37,34,32,30,28,27,24,23,21,19,19,17,16,15,14,13,13,12,11,11,11,10,10,10,11,11,11,12,13,13,14,15,16,17,19,19,21,23,24,27,28,30,32,34,37,38,41,43,46,49,51,54,57,60,64,65,69,72,75,79,81,84,88,92,96,98,101,105,109, 113,115,119,123}; unsigned short new_DC, newduty; char t,ii; //writing and reading the above data from sine_table those values are used for creating the PWM signal output at the 3 pins of PORTC //and then used for ocntrolling the system /* writeEEPROM(unsigned char address, unsigned char sine_table[]) { unsigned char INTCON_SAVE;//To save INTCON register value EEADR = address; //Address to write for(t=0;t<=220;t++) { EEDATL =sine_table[t]; //writing data from the sine table array into memory EECON1.EEPGD = 0; //Selecting EEPROM Data Memory EECON1.WREN = 1; //Enable writing of EEPROM INTCON_SAVE=INTCON;//Backup INCON interupt register INTCON=0; //Diables the interrupt EECON2=0x55; //Required sequence for write to internal EEPROM EECON2=0xAA; //Required sequence for write to internal EEPROM EECON1.WR = 1; //Initialise write cycle INTCON = INTCON_SAVE;//Enables Interrupt EECON1.WREN = 0; //To disable write while(PIR2.EEIF == 0)//Checking for complition of write operation { EEADR = address; //Address to be read EECON1.EEPGD = 0;//Selecting EEPROM Data Memory EECON1.RD = 1; //Initialise read cycle return EEDATL; //Returning data } PIR2.EEIF = 0; //Clearing EEIF bit } } */ unsigned write_readEEPROM() { for (t=0; t<202; t++) EEPROM_write(0x80+t,t); EEPROM_write(0x02,0XAA); EEPROM_write(0x50,0x55); delay_us(100); for (ii=0;ii<2020;ii++){ CCPR1L=EEPROM_Read(0x80+t); } } void debounce() { delay_ms(300); } void main() { TRISA=0XFF; TRISC=0XF0; TRISB=0XF8; CCP1CON=0XC; PSTR1CON=0X1F; PR2=249; T2CON=4; T2CON.TMR2ON=1; T2CON.TMR2ON=1; //PWM1CON= For dead time programming PWM1_init(15000); //Initialise PWM at 15KHZ while(1){ new_dc=0; if(!(PORTA.B1)){ debounce(); if(new_dc<249) new_dc=new_dc+5; //increment duty cycle PORTC.B3=0; } if(!PORTA.B2){ debounce(); if(new_dc!=0) new_dc=new_dc-5; //decrement duty cycle PORTC.B3=1; delay_ms(1000); } if(newduty!=new_dc) { newduty=new_DC; PWM1_Start(); PWM1_set_duty(newduty) ; } } }
Also could you please help me to make the phase shift of 120 degrees for those signals
I strongly suggest you make some experiments with one phase to taste the working principle of the PWM module driving the lags of the bridge, and just after add the 2 other phases. After achieving that, you could perform the optimization of the memory usage, scanning its content in the up/down directions instead of recording the full range. As Easyrider mentioned above, there is a symmetry which allows doing that.
sbit UP at RB5_bit; sbit DOWN at RB4_bit;
const unsigned char sine_table[33] = {0,24,48,69,88,104,115,123,125,123,115,104,88,69,48,24,0,-24,-48,-69,-88,-104,-115,-123,-125,-123,-115,-104,-88,-69,-48,-24,0,};
unsigned short new_DC, newduty;
char t,ii;
void debounce()
{
delay_ms(300);
}
void main()
{
TRISA=0XFF;
TRISC=0XF0;
TRISB=0XF8;
CCP1CON=0XC;
PSTR1CON=0X1F;
PR2=249;
T2CON=4;
T2CON.TMR2ON=1;
T2CON.TMR2ON=1;
//PWM1CON= For dead time programming
PWM1_init(15000); //Initialise PWM at 15KHZ
while(1){
new_dc=0;
if(!(PORTA.B5)){
debounce();
if(new_dc<249)
new_dc=new_dc+5; //increment duty cycle
}
if(!PORTA.B4){
debounce();
if(new_dc!=0)
new_dc=new_dc-5; //decrement duty cycle
}
if(newduty!=new_dc)
{
newduty=new_DC;
PWM1_Start();
PWM1_set_duty(sine_table[newduty]) ;
}
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?