Good day!
I saw this code in the internet for Single Phase AC Motor Speed Control using TRIAC. The existing project used AVR microcontroller. I would like to ask help from anyone here who's familiar/knowledgeable in both PIC and AVR in transferring and translating it to PIC18 (both are written in C). The schematic of the project is attached here.
View attachment AC Motor speed controller.pdf
Here's the code..
#include<reg51.h>
#define bargraph P2
sbit led1 = P3^7;
sbit led2 = P3^6;
sbit s1 = P1^0;
sbit s2 = P1^1;
sbit s3 = P1^2;
sbit pulse = P0^0;
/*code unsigned char timer_l[10]={0x38,0x3c,0x69,0x81,0x99,0xb1,0xc9,0xe1,0x94,0x11};
code unsigned char timer_h[10]={0xfc,0xf6,0xf4,0xf0,0xec,0xe8,0xe4,0xe0,0xdf,0xd9};*/
code unsigned char timer_l[10]={0x38,0x3c,0x69,0x81,0x78,0xb1,0xc9,0xe1,0x94,0x11};
code unsigned char timer_h[10]={0xfc,0xf6,0xf4,0xf0,0xec,0xe8,0xe4,0xe0,0xdf,0xd9};
code unsigned char fonts[10]={0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00};
unsigned char speed;
bit on=0;
void delay_ms (unsigned int);
void decangle(void) //decrease delay by 1ms
{
if(speed>0)
{
speed--; // decrease delay
if(speed!=8)
bargraph=fonts[speed];
}
}
void incangle(void) // increase delay by 1 ms
{
if(speed<9)
{
speed++;
bargraph=fonts[speed];
}
}
void tx (unsigned char x)
{
SBUF=x;
while(TI==0);
TI=0;
}
void int0(void) interrupt 0 // external interrupt 0 subroutine
{
led1=~led1; // led is on
if(on==1)
{
if(speed==0)
{
pulse=1; // oFF the triace permenately
return;
}
else if(speed==9)
{
pulse=0; // on the triace permenately
return;
}
else
{
pulse=1;
TL0 = timer_l[9-speed];
TH0 = timer_h[9-speed];
TR0 = 1;
}
}
else
pulse=1;
}
void timer0 (void) interrupt 1
{
TR0=0;
pulse=0;
}
void int1(void) interrupt 2 // external interrupt 1 subroutine
{
if(s1==0) // for first key
on=~on; // increase counter
else if(s2==0) // for second key
incangle(); // increase phase angle decrease power
else if(s3==0) // for third key
decangle(); // decrease phase angle increase power
delay_ms(300);
}
void uart(void)
{
SCON = 0x50;
TH1 = 0xF3;
TL1 = 0xF3;
TR1 = 1;
}
void init(void)
{
TMOD=0x21; // initialize timer0 as 16 bit timer , timer1 as 16 bit counter
IT0=IT1=1; // making edge trigger
bargraph=0xFF; // making all leds off
EX0=EX1=ET0=1; // enable interupts of timer0 abd external interrupt 0 & 1
EA=1;
}
void delay_ms (unsigned int count)
{
unsigned int i;
while(count) {
i = 115;
while(i>0) i--;
count--;
}
}
void send(unsigned char *str) // sending a whole string to port
{
while(*str)
{
tx(*str++);
delay_ms(2);
}
}
void main(void)
{
unsigned char a;
init();
uart();
while(1) // continuous loop
{
if(on==0)
led2=1;
else
led2=0;
if(RI==1)
{
a=SBUF;
RI=0;
switch(a)
{
case 'A':
speed=0;
send("Motor is off\r\n");
bargraph=fonts[speed];
break;
case 'B':
speed=1;
send("Level 1\r\n");
bargraph=fonts[speed];
break;
case 'C':
speed=2;
send("Level 2\r\n");
bargraph=fonts[speed];
break;
case 'D':
speed=3;
send("Level 3\r\n");
bargraph=fonts[speed];
break;
case 'E':
speed=4;
send("Level 4\r\n");
bargraph=fonts[speed];
break;
case 'F':
speed=5;
send("Level 5\r\n");
bargraph=fonts[speed];
break;
case 'G':
speed=6;
send("Level 6\r\n");
bargraph=fonts[speed];
break;
case 'H':
speed=7;
send("Level 7\r\n");
bargraph=fonts[speed];
break;
case 'I':
speed=8;
send("Level 8\r\n");
bargraph=fonts[speed];
break;
case 'J':
speed=9;
send("Full \r\n");
bargraph=fonts[speed];
break;
default:
on=~on;
if(on==0)
send("Power is disabled\r\n");
else
send("Power is enabled\r\n");
break;
}
}
}
}
Your help is very much appreciated.