TAPII
Newbie level 4
Below is the code i have developed to control few elements n devices using countdown counter!!Using MPLAB i can compile it well!!bt when i try to similate in proteus it isnt working!!Below is the code:
#include <pic.h>
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 20000000 //Frequecy of the crystal used in Sk40C
#endif
//===============configuration==============================
__CONFIG (0x3F32);
//===============define IO port=============================
#define POWERLED RA0 //Indicator of Power Supply (Yellow LED)
#define ALARM RA1 //LED illuminates as timer reaches zero after countdown (Blue LED)
#define SW1 RB0 //On and Off
#define SW2 RB1 //Start and Pause
#define DIP PORTC //DIP switch as input for the timer system
#define DISPLAY PORTD //Connection to the two 7-segment displays
#define DCMOTOR RE0 //DC Motor connection (Red LED)
//==============FUNCTION PTOTOTYPE=========================
void motor_full(void); //Subroutine for DC Motor at full speed
void motor_half(void); //Subroutine for DC Motor at lesser speed
void motor_stop(void); //Subroutine for DC Motor to stop
void delay(unsigned short k); //Subroutine for delay by counting down from specified
//====================MAIN================================
void main()
{
TRISA=0b00000000;
TRISB=0b00000011; //Bit 0 and Bit 1 are inputs
TRISC=0b11111111; //Bit 0-7 are inputs
TRISD=0b00000000; //Bit 0-7 are outputs
TRISE=0b00000000;
int i;
i=PORTC; //Declaring int i as PORTC (DIP switch input)
//=================================================================================================
if(SW1==0 && SW2==1) //If power switch is off and other switch is start then
{
POWERLED=0; //LED won't illuminate
i=0b00000000;
DISPLAY=0b00000000; //7 segment display would show 00
}
//=================================================================================================
else if(SW1==0 && SW2==0) //If power switch is off and other switch is pause then
{
POWERLED=0; //LED won't illuminate
i=0b00000000;
DISPLAY=0b00000000; //7 segment display would show 00
}
//=================================================================================================
else if(SW1==1 && SW2==1) //If power switch is on and other switch is start then
{
POWERLED=1; //LED would illuminate
if(i>99) //If DIP switch input is more than 99 decimal value then
{
i=0b01100011; //Consider the DIP switch to be 99 decimal value, more than 99 isn't allowed
while(i>0) //While the value of i is above 45 decimal value then (looping)
{
if(i>45)
{
i=i--; //Decrement counting
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
delay(900000); //Delay after each decrement
motor_full(); //DC Motor should run at full speed
}
//*************************************************************************************************
else(i<=45 && i>0) //While the value of i is below and equal to 45 decimal and above zero decimal value then (looping)
{
i=i--; //Decrement counting
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
delay(900000); //Delay after each decrement
motor_half(); //DC Motor should run at lesser speed
}
}
//*************************************************************************************************
if(i==0) //While the value of i is equal to zero decimal value then (looping)
{
DISPLAY=0b00000000; //Show 00 on the 7-segment displays
motor_stop(); //DC Motor should stop rotating
ALARM=1; //LED illuminates to know countdown has reached 00 or wash is over
}
}
//*************************************************************************************************
else(i<=99) //If DIP switch input is less than or equal to 99 decimal value then
{
i=i; //DIP switch's value is the value we assign to it
while(i>0) //While the value of i is above 45 decimal value then (looping)
{
if(i>45)
{
i=i--; //Decrementing the value assigned on the switch
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
delay(900000); //Delay after each decrement
motor_full(); //DC Motor should run at full speed
}
//*************************************************************************************************
else(i<=45 && i>0) //While the value of i is below and equal to 45 decimal and above zero decimal value then (looping)
{
i=i--; //Decrement counting
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
delay(900000); //Delay after each decrement
motor_half(); //DC Motor should run at lesser speed
}
}
//*************************************************************************************************
if(i==0) //While the value of i is equal to zero decimal value then (looping)
{
DISPLAY=0b00000000; //Show 00 on the 7-segment displays
motor_stop(); //DC Motor should stop rotating
ALARM=1; //LED illuminates to know countdown has reached 00 or wash is over
}
}
}
//=================================================================================================
else(SW1==1 && SW2==0) ////If power switch is on and other switch is pause then
{
POWERLED=1;
if(i>99) //If DIP switch input is more than 99 decimal value then
{
i=0b01100011; //Consider the DIP switch to be 99 decimal value, more than 99 isn't allowed
while(i>0) //While the value of i is above 45 decimal value then (looping)
{
if(i>45)
{
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
motor_stop(); //DC Motor should run at full speed
}
//*************************************************************************************************
else(i<=45 && i>0) //While the value of i is below and equal to 45 decimal and above zero decimal value then (looping)
{
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
motor_stop(); //DC Motor should run at lesser speed
}
}
//*************************************************************************************************
if(i==0) //While the value of i is equal to zero decimal value then (looping)
{
DISPLAY=0b00000000; //Show 00 on the 7-segment displays
motor_stop(); //DC Motor should stop rotating
ALARM=1; //LED illuminates to know countdown has reached 00 or wash is over
}
}
//*************************************************************************************************
else(i<=99) //If DIP switch input is less than or equal to 99 decimal value then
{
i=i; //DIP switch's value is the value we assign to it
while(i>0) //While the value of i is above 45 decimal value then (looping)
{
if(i>45)
{
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
motor_stop(); //DC Motor should run at full speed
}
//*************************************************************************************************
else(i<=45 && i>0) //While the value of i is below and equal to 45 decimal and above zero decimal value then (looping)
{
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
motor_stop(); //DC Motor should run at lesser speed
}
}
//*************************************************************************************************
if(i==0) //While the value of i is equal to zero decimal value then (looping)
{
DISPLAY=0b00000000; //Show 00 on the 7-segment displays
motor_stop(); //DC Motor should stop rotating
ALARM=1; //LED illuminates to know countdown has reached 00 or wash is over
}
}
}
}
//==================Motor full========================
void motor_full(void)
{
PORTE=0x01;
}
//==================Motor half========================
void motor_half(void)
{
PORTE=0x01;
__delay_ms(15);
CLRWDT();
PORTE=0x00;
__delay_ms(7);
CLRWDT();
}
//==================Motor stop========================
void motor_stop(void)
{
PORTE=0x00;
}
//=================Delay==============================
void delay(unsigned short k)
{
for(;k>0;k--);
}
---------- Post added at 00:26 ---------- Previous post was at 00:08 ----------
here is the image from proteus!!
https://obrazki.elektroda.pl/15_1306952618.jpg
https://obrazki.elektroda.pl/15_1306952618_thumb.jpg
#include <pic.h>
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 20000000 //Frequecy of the crystal used in Sk40C
#endif
//===============configuration==============================
__CONFIG (0x3F32);
//===============define IO port=============================
#define POWERLED RA0 //Indicator of Power Supply (Yellow LED)
#define ALARM RA1 //LED illuminates as timer reaches zero after countdown (Blue LED)
#define SW1 RB0 //On and Off
#define SW2 RB1 //Start and Pause
#define DIP PORTC //DIP switch as input for the timer system
#define DISPLAY PORTD //Connection to the two 7-segment displays
#define DCMOTOR RE0 //DC Motor connection (Red LED)
//==============FUNCTION PTOTOTYPE=========================
void motor_full(void); //Subroutine for DC Motor at full speed
void motor_half(void); //Subroutine for DC Motor at lesser speed
void motor_stop(void); //Subroutine for DC Motor to stop
void delay(unsigned short k); //Subroutine for delay by counting down from specified
//====================MAIN================================
void main()
{
TRISA=0b00000000;
TRISB=0b00000011; //Bit 0 and Bit 1 are inputs
TRISC=0b11111111; //Bit 0-7 are inputs
TRISD=0b00000000; //Bit 0-7 are outputs
TRISE=0b00000000;
int i;
i=PORTC; //Declaring int i as PORTC (DIP switch input)
//=================================================================================================
if(SW1==0 && SW2==1) //If power switch is off and other switch is start then
{
POWERLED=0; //LED won't illuminate
i=0b00000000;
DISPLAY=0b00000000; //7 segment display would show 00
}
//=================================================================================================
else if(SW1==0 && SW2==0) //If power switch is off and other switch is pause then
{
POWERLED=0; //LED won't illuminate
i=0b00000000;
DISPLAY=0b00000000; //7 segment display would show 00
}
//=================================================================================================
else if(SW1==1 && SW2==1) //If power switch is on and other switch is start then
{
POWERLED=1; //LED would illuminate
if(i>99) //If DIP switch input is more than 99 decimal value then
{
i=0b01100011; //Consider the DIP switch to be 99 decimal value, more than 99 isn't allowed
while(i>0) //While the value of i is above 45 decimal value then (looping)
{
if(i>45)
{
i=i--; //Decrement counting
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
delay(900000); //Delay after each decrement
motor_full(); //DC Motor should run at full speed
}
//*************************************************************************************************
else(i<=45 && i>0) //While the value of i is below and equal to 45 decimal and above zero decimal value then (looping)
{
i=i--; //Decrement counting
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
delay(900000); //Delay after each decrement
motor_half(); //DC Motor should run at lesser speed
}
}
//*************************************************************************************************
if(i==0) //While the value of i is equal to zero decimal value then (looping)
{
DISPLAY=0b00000000; //Show 00 on the 7-segment displays
motor_stop(); //DC Motor should stop rotating
ALARM=1; //LED illuminates to know countdown has reached 00 or wash is over
}
}
//*************************************************************************************************
else(i<=99) //If DIP switch input is less than or equal to 99 decimal value then
{
i=i; //DIP switch's value is the value we assign to it
while(i>0) //While the value of i is above 45 decimal value then (looping)
{
if(i>45)
{
i=i--; //Decrementing the value assigned on the switch
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
delay(900000); //Delay after each decrement
motor_full(); //DC Motor should run at full speed
}
//*************************************************************************************************
else(i<=45 && i>0) //While the value of i is below and equal to 45 decimal and above zero decimal value then (looping)
{
i=i--; //Decrement counting
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
delay(900000); //Delay after each decrement
motor_half(); //DC Motor should run at lesser speed
}
}
//*************************************************************************************************
if(i==0) //While the value of i is equal to zero decimal value then (looping)
{
DISPLAY=0b00000000; //Show 00 on the 7-segment displays
motor_stop(); //DC Motor should stop rotating
ALARM=1; //LED illuminates to know countdown has reached 00 or wash is over
}
}
}
//=================================================================================================
else(SW1==1 && SW2==0) ////If power switch is on and other switch is pause then
{
POWERLED=1;
if(i>99) //If DIP switch input is more than 99 decimal value then
{
i=0b01100011; //Consider the DIP switch to be 99 decimal value, more than 99 isn't allowed
while(i>0) //While the value of i is above 45 decimal value then (looping)
{
if(i>45)
{
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
motor_stop(); //DC Motor should run at full speed
}
//*************************************************************************************************
else(i<=45 && i>0) //While the value of i is below and equal to 45 decimal and above zero decimal value then (looping)
{
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
motor_stop(); //DC Motor should run at lesser speed
}
}
//*************************************************************************************************
if(i==0) //While the value of i is equal to zero decimal value then (looping)
{
DISPLAY=0b00000000; //Show 00 on the 7-segment displays
motor_stop(); //DC Motor should stop rotating
ALARM=1; //LED illuminates to know countdown has reached 00 or wash is over
}
}
//*************************************************************************************************
else(i<=99) //If DIP switch input is less than or equal to 99 decimal value then
{
i=i; //DIP switch's value is the value we assign to it
while(i>0) //While the value of i is above 45 decimal value then (looping)
{
if(i>45)
{
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
motor_stop(); //DC Motor should run at full speed
}
//*************************************************************************************************
else(i<=45 && i>0) //While the value of i is below and equal to 45 decimal and above zero decimal value then (looping)
{
DISPLAY=i; //Show the value on 7-segment displays as count decreases simultaneously
motor_stop(); //DC Motor should run at lesser speed
}
}
//*************************************************************************************************
if(i==0) //While the value of i is equal to zero decimal value then (looping)
{
DISPLAY=0b00000000; //Show 00 on the 7-segment displays
motor_stop(); //DC Motor should stop rotating
ALARM=1; //LED illuminates to know countdown has reached 00 or wash is over
}
}
}
}
//==================Motor full========================
void motor_full(void)
{
PORTE=0x01;
}
//==================Motor half========================
void motor_half(void)
{
PORTE=0x01;
__delay_ms(15);
CLRWDT();
PORTE=0x00;
__delay_ms(7);
CLRWDT();
}
//==================Motor stop========================
void motor_stop(void)
{
PORTE=0x00;
}
//=================Delay==============================
void delay(unsigned short k)
{
for(;k>0;k--);
}
---------- Post added at 00:26 ---------- Previous post was at 00:08 ----------
here is the image from proteus!!
https://obrazki.elektroda.pl/15_1306952618.jpg
https://obrazki.elektroda.pl/15_1306952618_thumb.jpg