janosandi
Full Member level 4
- Joined
- Jan 23, 2010
- Messages
- 210
- Helped
- 2
- Reputation
- 4
- Reaction score
- 2
- Trophy points
- 1,298
- Activity points
- 2,788
hello Guys
i've made a simple controlller for 3 stages elevator
the main problem is tht when testing on proteus it works great & even on real board its ok but sometimes it make errors.
Errors like not responding to emergency push button(which its normally cloesed) OR
converting the input pins to outputs & not respond to limit switch & it was a disaster
i've thought of ading wdt to my project & make the emergency switch cut off the main power of the pic
im using pic 16f877 with 4 mhz crystal which it drives 2 relays to drive 2 24v contactos
with 3 position select push buttons & position limit switches with 2 error switchs in case the main limit switch has failed
in proteus it works great
would you check my code & give me any advice
Thx guys
John
i've made a simple controlller for 3 stages elevator
the main problem is tht when testing on proteus it works great & even on real board its ok but sometimes it make errors.
Errors like not responding to emergency push button(which its normally cloesed) OR
converting the input pins to outputs & not respond to limit switch & it was a disaster
i've thought of ading wdt to my project & make the emergency switch cut off the main power of the pic
im using pic 16f877 with 4 mhz crystal which it drives 2 relays to drive 2 24v contactos
with 3 position select push buttons & position limit switches with 2 error switchs in case the main limit switch has failed
in proteus it works great
would you check my code & give me any advice
Thx guys
John
Code:
bit cnt;
void drvup(){
if(PORTB.B4 == 0 && PORTB.B3 == 1 && PORTC.B0 == 1 && PORTC.B1 == 1) { //emergency = false & up limit = false & error switches are off
Cnt = 1;
do {
PORTD = 1; //drive up relay
}
while(PORTB.B3 == 1 && PORTB.B4 == 0 && PORTC.B0 == 1 && PORTC.B1 == 1&& Cnt ==1);
}
}
void drvmid(){
if(PORTB.B4 == 0 && PORTB.B3 == 0 && PORTC.B0 == 1 && PORTC.B1 == 1 ) { //emergency = false & up limit = false
Cnt = 1;
do {
PORTD = 2; //drive down relay
}
while(PORTB.B1 == 1 && PORTB.B2 == 1 && Cnt ==1 && PORTC.B0 == 1 && PORTC.B1 == 1 && PORTB.B4 == 0);
}
if(PORTB.B4 == 0 && PORTB.B1 == 0 && PORTC.B0 == 1 && PORTC.B1 == 1) { //emergency = false & up limit = false
Cnt = 1;
do {
PORTD = 1; //Drive up relay
}
while(PORTB.B3 == 1 && PORTB.B2 == 1 && Cnt ==1 && PORTC.B0 == 1 && PORTC.B1 == 1 && PORTB.B4 == 0);
}
}
void drvdwn(){
if(PORTB.B4 == 0 && PORTB.B1 == 1 && PORTC.B0 == 1 && PORTC.B1 == 1) { //emergency = false & up limit = false
Cnt = 1;
do {
PORTD = 2; // Drive down relay
}
while(PORTB.B1 == 1 && PORTB.B4 == 0 && PORTC.B0 == 1 && PORTC.B1 == 1&& Cnt ==1);
}
}
void drvdwn1(){
PORTD = 2; // drive down relay
delay_ms(50);
}
void drvup1(){
PORTD = 1; // drive up relay
delay_ms(50);
}
void emer(){
if(PORTD == 2){
PORTB.B1 = 0;
}
}
void error(){
PORTD = 4;
Cnt = 0;
}
void errorup(){
if(PORTB.B5 == 0) { // Down push button
drvdwn1();
}
PORTD = 4; // error lamp
Cnt = 0;
}
void errordwn(){
if(PORTB.B7 == 0) {
drvup1();
}
PORTD = 4;
Cnt = 0;
}
void InitTimer1() {
//T1CON = 0x01;
T1CON = 0x01;
TMR1IF_bit = 0;
TMR1H = 0xF8;
TMR1L = 0x30;
TMR1IE_bit = 1;
INTCON = 0xC0;
}
void Interrupt(){
if(TMR1IF_bit) {
TMR1H = 0xF8;
TMR1L = 0x30;
PORTD = 0x00;
}
TMR1IF_bit = 0;
if(PORTB.B7 == 0) { //up push button
drvup();
}
if(PORTB.B6 == 0) { // mid push button
drvmid();
}
if(PORTB.B5 == 0) { // down push button
drvdwn();
}
if(PORTB.B4 == 0) { //emergency push button
emer();
}
if(PORTC.B0 == 0) { // up error limit switch
errorup();
}
if(PORTC.B1 == 0) { // down error limit switch
errordwn();
}
if(PORTB.B1 ==0 && PORTB.B3 ==0){ //if down limit & up limit switches are pressed together
error();
}
if(PORTB.B1 ==0 && PORTB.B2 ==0){ //if down limit & mid limit switches are pressed together
error();
}
if(PORTB.B2 ==0 && PORTB.B3 ==0){ //if mid limit & up limit switches are pressed together
error();
}
}
void main() {
CMCON = 0x07;
ADCON1 = 0x87;
TRISA = 0x00;
TRISB = 0xFF;
TRISC = 0x03;
TRISD = 0x00;
TRISE = 0x00;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
PORTE = 0x00;
InitTimer1();
}