Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Controlling devices using countdown counter

Status
Not open for further replies.

TAPII

Newbie level 4
Joined
May 30, 2011
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,393
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
 

where r the include "header"files or code
 

It does not appear that you have disable the ADC capabilities on PORTA and ensured they are digital I/O.

Download the 16F87XA data sheet:

PIC16F87XA Data Sheet

Quote from PICX16F87XA Datasheet

PORTA is a 6-bit wide, bidirectional port. The corresponding
data direction register is TRISA. Setting a
TRISA bit (= 1) will make the corresponding PORTA pin
an input (i.e., put the corresponding output driver in a
High-Impedance mode). Clearing a TRISA bit (= 0) will
make the corresponding PORTA pin an output (i.e., put
the contents of the output latch on the selected pin).
Reading the PORTA register reads the status of the
pins, whereas writing to it will write to the port latch. All
write operations are read-modify-write operations.
Therefore, a write to a port implies that the port pins are
read, the value is modified and then written to the port
data latch.
Pin RA4 is multiplexed with the Timer0 module clock
input to become the RA4/T0CKI pin. The RA4/T0CKI
pin is a Schmitt Trigger input and an open-drain output.
All other PORTA pins have TTL input levels and full
CMOS output drivers.
Other PORTA pins are multiplexed with analog inputs
and the analog VREF input for both the A/D converters
and the comparators. The operation of each pin is
selected by clearing/setting the appropriate control bits
in the ADCON1 and/or CMCON registers.

On a Power-on Reset, these pins are configured
as analog inputs and read as ‘0’.
The comparators are in the off (digital)
state

The TRISA register controls the direction of the port
pins even when they are being used as analog inputs.
The user must ensure the bits in the TRISA register are
maintained set when using them as analog inputs.

Assembly Routine to Configure PORTA Pins

BCF STATUS, RP0 ;
BCF STATUS, RP1 ; Bank0
CLRF PORTA ; Initialize PORTA by
; clearing output
; data latches
BSF STATUS, RP0 ; Select Bank 1
MOVLW 0x06 ; Configure all pins
MOVWF ADCON1 ; as digital inputs
MOVLW 0xCF ; Value used to
; initialize data
; direction
MOVWF TRISA ; Set RA<3:0> as inputs
; RA<5:4> as outputs
; TRISA<7:6>are always
; read as '0'.

You can configure the PORTA using C, the above code should give you an example of its configuration.

---------- Post added at 06:57 ---------- Previous post was at 06:43 ----------

By the way, PORTE has the same issue, it is configured as ADC inputs upon powerup as well.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top