|
楼主 |
发表于 2010-5-30 04:17:37
|
显示全部楼层
here is the code.
=========code==============
#include <htc.h>
#include <string.h>
#include "lcd_3wi.h"
#include "delay.h"
__CONFIG(MCLRDIS & WDTDIS & BORDIS & PWRTEN & INTIO);
#define LED_PORT PORTA //leds on porta
#define LED_A (1<<5) //anode on pin0
#define LED_K (1<<4) //cathode on pin2
#define LED_SET(bits) LED_PORT |= (bits) //set bits
#define LED_CLR(bits) LED_PORT &=~(bits) //clear bits
#define LED_FLP(bits) LED_PORT ^= (bits) //flip bits
#define LED_IN(bits) TRISA |= (bits) //bits as input
#define LED_OUT(bits) TRISA &=~(bits) //bits as output
#define LED_EMIT(a, k) LED_OUT((a) | (k)), LED_SET(a), LED_CLR(k) //forward bias the led
#define LED_CHARGE(a, k) LED_OUT((a) | (k)), LED_CLR(a), LED_SET(k); //reverse charge up the led. led is off
#define LED_DISCHARGE(a, k) LED_OUT(a), LED_CLR(a), LED_IN(k); //discharge the led
#define LED_MAX_COUNT 0xff //max cycle count for the led_detect() routine
#define LED_PRESSED 0x1f //if count more than LEd_PRESSED, consider the led has been pressed
#define KEY_DLY 5 //key delay, 5ms
#define LED_BRIGHTNESS_BRIGHT 90 //bright=90% dc
#define LED_BRIGHTNESS_DIM 1 //dim=1% dc
unsigned char vRAM[17]; //lcd buffer
const unsigned char str0[]="16F684 LEDDiode1";
const unsigned char str1[]="t = ";
const unsigned char key_pressed[] ="Key Pressed!";
const unsigned char key_not_pressed[]="Key NOT Pressed!";
unsigned long t1_count=0;
void mcu_init(void ) { //reset the mcu
ANSEL=0x00; //all pins gpio
CMCON0=0x07; //analog comparators off
IRCF2=1, IRCF1=1, IRCF0=0; //running at 4mhz
}
void ultoa(char *s, unsigned long ul, unsigned char length) { //convert unsigned long to a string, 3 dps
unsigned char i;
i=length;
do {
// if (i==(length-3)) s[i--]='.';
s[i--]=ul % 10 + '0';
ul = ul / 10;
} while (ul);
}
unsigned long led_detect(unsigned char led_a, unsigned char led_k) { //return the cycle count
unsigned long tmp;
LED_CHARGE(led_a, led_k); delay_ms(1); //charge up the led
LED_DISCHARGE(led_a, led_k); //discharge the led
tmp=0;
while ((LED_PORT & led_k) && (tmp < LED_MAX_COUNT)) tmp++; //wait for LED_K to fall
// while (LED_PORT & led_k) tmp++; //wait for LED_K to fall
// return tmp; //return the cycle count
return tmp>LED_PRESSED; //or if led ahs been pressed
}
void simple_pwm(unsigned char dc, unsigned char led_a, unsigned char led_k) {//simple pwm. dc=0..99 percent
unsigned char repeat, i;
dc>>=4; //dc's highest 4 bits
for (repeat=0; repeat < LED_MAX_COUNT >> 4; repeat++) {
LED_EMIT(led_a, led_k); //turn on the led
for (i=0, LED_EMIT(led_a, led_k); i<100; i++)
if (i==dc) {LED_CHARGE(led_a, led_k);} //turn off the led;
}
}
void
main(void)
{ unsigned long t1;
unsigned char i=0;
unsigned char key_flag=0; //key not pressed initially
mcu_init(); //initialize the mcu
lcd_init(); //initialize the lcd
strcpy(vRAM, str0); lcd_display(LCD_Line0, vRAM);
while (1){
if (led_detect(LED_A, LED_K)) {
key_flag ^=1;
delay_ms(KEY_DLY);}//flip key status
if (key_flag) {
strcpy(vRAM, key_pressed); lcd_display(LCD_Line1, vRAM);
simple_pwm(LED_BRIGHTNESS_BRIGHT, LED_A, LED_K);
}
else {
strcpy(vRAM, key_not_pressed); lcd_display(LCD_Line1, vRAM);
simple_pwm(LED_BRIGHTNESS_DIM, LED_A, LED_K);
}
// LED_EMIT(LED_A, LED_K); delay_us(t1>>4);
// simple_pwm(t1, LED_A, LED_K);
//TODO Auto-generated main function
}
}
=========end of code================
don't worry about the led stuff. you can comment it out.
when you touch the led, it will toggle between "bright" (90% duty cycle) and "dim" (1% duty cycle). the same principle. |
|