Minggu, 20 April 2008

counter

Scanning 7-Segment & Keypad
Since the output buffer of P1 can sink 20mA (each output pin, but maximum IOL for all outputs was limited at 80mA), thus we can use P1 to drive LED display directly. As shown in the circuit, Two common-anode 7-segment LEDs are connected to P1 with 180 Ohms current limiting resistor. Each segment of two LED are tied in parallel to P1. Q1 and Q2 are activated by logic low of P3.0 and P3.1, sourcing +5V to common anode pins. P3.4 read logic low if either S1 or S2 was pressed while scanning period have made.










________________________________________

/*
* 7-seg.c
* Driving 2-digit 7-segment Common Anode LED & keypad
* Copyright (C) 1999 Wichit Sirichote
* compiled with Dunfield Micro-C for 8051 Release 3.2
* c:\mc\cc51 7-seg -i h=c:\mc m=t
*/
#include c:\mc\8051io.h /* include i/o header file */
#include c:\mc\8051reg.h
extern register char cputick; // cputick was incremented every 10ms
register unsigned char flag1;
unsigned register char sec,digit,buffer[2];
register char key;
char convert[10] = {0x3F,0x0c,0x76,0x5e,0x4d,0x5b,0x7b,0x0e,0x7f,0x5f};

/* my LED's segment pin designation (differs from standard)
b
__
a|__| c
f| | d
--
e
*/
#define setValue 99
main()
{
flag1 = 0;
sec = setValue;
timeToBuffer();
serinit(9600); // set timer0 to be 16 bit counter
while(1){
while(cputick < 10)
scanLED();
// execute the following functions every 100ms
cputick = 0;
timeToBuffer();
keyexe();
countdown();
}
}

scanLED() /* scan 2-digit LED and 2-key switch, if key pressed key = 0-1
else key = -1 */
{
int i;
digit = 0x02; // scan code 00000010
key = -1;
for( i = 0; i < 2; i++) /* 2-DIGIT scanning */
{
P3 = ~digit; /* send complement[digit] */
P1 = ~buffer[i]; /* send complement[segment] */
delay(1); /* delay 1ms */
P1 = 0xff; /* off LED */
if ((P3 & 0x10) == 0) /* if key pressed P3.4 became low */
key = i; /* save key position to key variable */
digit>>=1; /* next digit */
}
}
timeToBuffer() // converts binary data in sec to 7-segment pattern
{
buffer[0] = convert[sec%10];
buffer[1] = convert[sec/10];
}
countdown()
{
if ((flag1 & 0x02) != 0)
sec--;
if (sec == 0 )
flag1 &= ~0x02; // clear run bit
}
keyexe()
{
if (key != -1)
{
switch(key){
case (0): /* key position 0 */
reset(); /* service key 0 */
break;
case (1): /* key position 1 */
run(); /* service key 1 */
}
}
}
reset()
{
sec = setValue; // reload set value
timeToBuffer();
flag1 &= ~0x02; // stop counting down
}
run()
{
if (sec != 0)
flag1 |= 0x02; // start counting down
}

stepper


Driving Stepper

M1 is a stepper taken from an old disk drive. There are five pins, i.e., common, coil 1, 2, 3 and 4. Resistance measured between common pin and each coil is about 75 Ohms. Driving current for each coil is then needed about 60mA at +5V supply. A darlington transistor array, ULN2003 is used to increase driving capacity of the 2051 chip. Each output provides 500mA max at 50V. P1.4 to P1.7, four output pins are connected to the input of the ULN2003 as shown in the circuit. Four 4.7k resistors help the 2051 to provide more sourcing current from the +5V supply. The serial port is optional for your exercises. Many have provided useful technical, and application of using stepper, see the links below.







/*
* STEPPER.C
* sweeping stepper's rotor cw and cww 400 steps
* Copyright (c) 1999 by W.Sirichote
*/
#include c:\mc51\8051io.h /* include i/o header file */
#include c:\mc51\8051reg.h
register unsigned char j,flag1,temp;
register unsigned int cw_n,ccw_n;
unsigned char step[8]={0x80,0xc0,0x40,0x60,0x20,0x30,0x10,0x90}
#define n 400
/* flag1 mask byte
0x01 run cw()
0x02 run ccw()
*/
main()
{
flag1=0;
serinit(9600);
disable(); /* no need timer interrupt */
cw_n = n; /* initial step number for cw */
flag1 |=0x01; /* initial enable cw() */
while(1){
{
tick_wait(); /* wait for 10ms elapsed */
energize(); /* round-robin execution the following tasks every 10ms */
cw();
ccw();
}
}
}
cw(){
if((flag1&0x01)!=0)
{
cw_n--; /* decrement cw step number */
if (cw_n !=0)
j++; /* if not zero increment index j */
else
{flag1&=~0x01; /* disable cw() execution */
ccw_n = n; /* reload step number to ccw counter */
flag1 |=0x02; /* enable cww() execution */
}
}
}
ccw(){
if((flag1&0x02)!=0)
{
ccw_n--; /* decremnent ccw step number */
if (ccw_n !=0)
j--; /* if not zero decrement index j */
else
{flag1&=~0x02; /* disable ccw() execution */
cw_n = n; /* reload step number to cw counter */
flag1 |=0x01; /* enable cw() execution */
}
}
}
tick_wait(){ /* cputick was replaced by simpler ASM code 10ms wait */
asm" JNB TCON.5,*"; /* wait for TF0 set */
asm" CLR TCON.5"; /* clear TF0 for further set */
asm" ORL TH0,#$DC"; /* reload TH0 with $DC, TL0 = 0 */
}
energize(){
P1 = step[(j&0x07)]; /* only step 0-7 needed */
}