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
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();
}
Errors like not responding to emergency push button(which its normally cloesed)
Do you have 100nF capacitors and some bigger electrolytic ones at your MCU pins?
Do you protect your MCU from relays by using diodes?
There is nothing in your code seemingly spending much time to process such as standard libraries, math calculation, neither any closed loop instruction inspecting SFR registers. In addition, although the delay time of few dozens mili-seconds could induce the idea of a small interval, its proportion to the overall code in terms of processing time isn’t negligible. Keep in mind that your code will do nothing ( read input, change output ) in the meantime delay is running. I strongly recommend you reduce or eliminate delay just to make a test to confirm or refuse such an hypotesis based on the real-world circuit.
yes sure i have 100nf capacitor & diodes on relays
Ok, but what about your layout ?
Please, tell us that you're not doing that on breadboard.
RESET and POWER CUT will switch the output pins to z-state. If you will take a look on schematics of real devices, all outputs always being protected with h/w logic like 74xxx (AND, NOR).
The better way is to use watchdog timer. In some cases even external one. And during initialisation analize reset vector source. If it is power cut or watchdog reset - do some logging.
You can't resume operation from same place. It is impossible with PIC and you are not making electronics for space. Your purpose is to block all movable parts in case of h/w or s/w malfunction. If your device is stucked, you only need to know how it happens. You don't need any other functionality because if it is stuck, that means that it is not working properly. If so, no reason to use it any more. Problem should be solved, not workaraunded.
// Init pressure sensor
BMP180_Struct.delay_func = vTaskDelay;
BMP180_Struct.ReadReg = I2C_ReadReg;
BMP180_Struct.WriteReg = I2C_WriteReg;
BMP180_Struct.Resolution = 3;
BMP180_Struct.I2C_Adrs = 0xEE;
Error.BMP180 = BMP180_Init(&BMP180_Struct);
if (Error.BMP180 == I2C_ADD_NOT_EXIST)
ErrorHandle(BMP180, "BMP180 does not answer!\r\n");
else if (Error.BMP180)
ErrorHandle(BMP180, "BMP180 unknown error!\r\n");
Just an example of my recent code:
Even if I check the sensor, I always print out some debug messages. Later on the debug will be switched off to reduce code size.Code:// Init pressure sensor BMP180_Struct.delay_func = vTaskDelay; BMP180_Struct.ReadReg = I2C_ReadReg; BMP180_Struct.WriteReg = I2C_WriteReg; BMP180_Struct.Resolution = 3; BMP180_Struct.I2C_Adrs = 0xEE; Error.BMP180 = BMP180_Init(&BMP180_Struct); if (Error.BMP180 == I2C_ADD_NOT_EXIST) ErrorHandle(BMP180, "BMP180 does not answer!\r\n"); else if (Error.BMP180) ErrorHandle(BMP180, "BMP180 unknown error!\r\n");
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?