Professional Documents
Culture Documents
/************************************************
* *
* COPYRIGHT (c) Blitzlogic Sdn. Bhd. *
* Author : Abraham Wong 21/1/2000 *
* *
* example of using WHILE loop construct *
* to drive 8 LEDS connected to port B *
* *
************************************************/
#include <16c84.h>
#FUSES XT,NOWDT,NOPROTECT,NOPUT
/* Use XT mode, No Watch Dog, No Code Protect, No
Power-up Timer */
main(){
while( TRUE )
{ /* forever loop using WHILE construct */
cnt = 0;
while ( cnt<8 )
{
port_b = value;
DELAY_MS(1000);
value = value << 1; /* shift left will put
0x01, 0x02, 0x04, 0x08, 0x10 */
cnt++; /* 0x20, 0x40, 0x80 to port_b */
}
}
}
****************************************/
#include <16c84.h>
main(){
byte cnt, right,num ;
for( ;; ){
for (right=1;right<3;right++){
port_a = right;
for (cnt=0;cnt<10;cnt++){
port_b = LED_MAP[cnt];
DELAY_MS(1000); /* one second delay */
}
}
}
}
#include <16c84.h>
main(){
char cnt, col;
for( ;; )
{
col = 1;
for(cnt = 0;cnt < 5;cnt++)
{
port_b = pat[cnt];
port_a = col;
delay_ms(1);
col<<=1;
}
}
}
#include <16F84.H>
#fuses XT,NOPROTECT,NOWDT
#use delay(clock=4000000)
#include
#include
main() {
char k;
lcd_init();
kbd_init();
lcd_putc("\fReady...\n");
while (TRUE) {
k=kbd_getc();
if(k!=0)
if(k=='*')
lcd_putc('\f');
else
lcd_putc(k);
}
}
byte lcd_read_byte() {
byte low,high;
set_tris_b(LCD_READ);
lcd.rw = 1;
delay_cycles(1);
lcd.enable = 1;
delay_cycles(1);
high = lcd.data;
lcd.enable = 0;
delay_cycles(1);
lcd.enable = 1;
delay_us(1);
low = lcd.data;
lcd.enable = 0;
set_tris_b(LCD_WRITE);
return( (high<<4) | low);
}
void lcd_init() {
byte i;
set_tris_b(LCD_WRITE);
lcd.rs = 0;
lcd.rw = 0;
lcd.enable = 0;
delay_ms(15);
for(i=1;i<=3;++i) {
lcd_send_nibble(3);
delay_ms(5);
}
lcd_send_nibble(2);
for(i=0;i<=3;++i)
lcd_send_byte(0,LCD_INIT_STRING[i]);
}
if(y!=1)
address=lcd_line_two;
else
address=0;
address+=x-1;
lcd_send_byte(0,0x80|address);
}
lcd_gotoxy(x,y);
lcd.rs=1;
value = lcd_read_byte();
lcd.rs=0;
return(value);
}
#ifdef
blue_keypad /////////////////////////////////////
For the blue keypad
#define COL0 (1 << 2)
#define COL1 (1 << 3)
#define COL2 (1 << 6)
#else ///////////////////////////////////////////////
/// For my keypad
#define COL0 (1 << 6)
#define COL1 (1 << 2)
#define COL2 (1 << 3)
// Keypad layout:
char const KEYS[4][3] = {{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}};
void kbd_init() {
#ifdef __PCM__
port_b_pullups(true); // If not PCM be sure to
use external pullups
#endif
}
char kbd_getc( ) {
static byte kbd_call_count;
static short int kbd_down;
static char last_key;
static byte col;
byte kchar;
byte row;
kchar='\0';
if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
switch (col) {
case 0 : set_tris_b(ALL_PINS&~COL0);
kbd=~COL0&ALL_PINS;
break;
case 1 : set_tris_b(ALL_PINS&~COL1);
kbd=~COL1&ALL_PINS;
break;
case 2 : set_tris_b(ALL_PINS&~COL2);
kbd=~COL2&ALL_PINS;
break;
}
if(kbd_down) {
if((kbd & (ALL_ROWS))==(ALL_ROWS)) {
kbd_down=false;
kchar=last_key;
last_key='\0';
}
} else {
if((kbd & (ALL_ROWS))!=(ALL_ROWS)) {
if((kbd & ROW0)==0)
row=0;
else if((kbd & ROW1)==0)
row=1;
else if((kbd & ROW2)==0)
row=2;
else if((kbd & ROW3)==0)
row=3;
last_key =KEYS[row][col];
kbd_down = true;
} else {
++col;
if(col==3)
col=0;
}
}
kbd_call_count=0;
}
set_tris_b(ALL_PINS);
return(kchar);
}
Giao tiep voi ltc1298
*****************************************************
************************/
#ifndef ADC_CS
#endif
void adc_init() {
output_high(ADC_CS);
}
delay_us(2);
for(i=0; i>1;
output_high(ADC_CLK);
delay_us(50);
output_low(ADC_CLK);
delay_us(50);
}
}
data=0;
for(i=0;i<<8)|l);
}
div=0x3330;
for(i=0;i<=4;i++) {
temp=data/div;
volts[i]=(byte)temp+'0';
if(i==0) {
volts[1]='.';
i++;
}
temp=div*(byte)temp;
data=data-temp;
div=div/10;
}
volts[i]='\0';
}
Example to read two A/D Channels of
LTC1298
/////////////////////////////////////////////////////
////////////////////
//// EX_AD12.C
////
////
////
//// This program will read both A/D channels and
display the ////
//// results as both a voltage and raw hex number
over the RS-232. ////
//// A reading is taken every second.
////
////
////
//// (C) Copyright 1996,1997 Custom Computer
Services ////
////
////
/////////////////////////////////////////////////////
////////////////////
#include <16F84.H>
#fuses HS,NOPROTECT,NOWDT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2)
#include
main() {
long int value;
adc_init();
printf("Sampling:\r\n");
do {
delay_ms(1000);
value = read_analog(0);
printf("\n\rCh0: ");
display_data( value );
value = read_analog(1);
printf(" Ch1: ");
display_data( value );
} while (TRUE);
#ifndef EEPROM_SDA
#endif
void init_ext_eeprom() {
output_low(eeprom_scl);
output_high(eeprom_sda);
}
i2c_start();
i2c_write(0xa0);
i2c_write(address);
i2c_write(data);
i2c_stop();
delay_ms(11);
}
byte read_ext_eeprom(byte address) {
byte data;
i2c_start();
i2c_write(0xa0);
i2c_write(address);
i2c_start();
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
return(data);
}
#include <16F84.H>
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2)
#include
#include <2402.C>
main() {
byte value,cmd;
EEPROM_ADDRESS address;
init_ext_eeprom();
do {
do {
printf("\r\nRead or Write: ");
cmd=getc();
cmd=toupper(cmd);
putc(cmd);
} while ( (cmd!='R') && (cmd!='W') );
printf("\n\rLocation: ");
#if sizeof(EEPROM_ADDRESS)==1
address = gethex();
#else
#if EEPROM_SIZE>0xfff
address = gethex();
#else
address = gethex1();
#endif
address = (address<<8)+gethex();
#endif
if(cmd=='R')
printf("\r\nValue:
%X\r\n",READ_EXT_EEPROM( address ) );
if(cmd=='W') {
printf("\r\nNew value: ");
value = gethex();
printf("\n\r");
WRITE_EXT_EEPROM( address, value );
}
} while (TRUE);
#include <16F84.H>
#fuses XT,NOWDT,NOPROTECT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2)
#define INTS_PER_SECOND 76 //
(20000000/(4*256*256))
main() {
byte start;
int_count=INTS_PER_SECOND;
set_rtcc(0);
setup_counters( RTCC_INTERNAL, RTCC_DIV_256);
enable_interrupts(RTCC_ZERO);
enable_interrupts(GLOBAL);
do {
} while (TRUE);
}
Example to create a pulse via
RTCC( timer0 )
/////////////////////////////////////////////////////
////////////////////
//// EX_PULSE.C
////
////
////
//// This program uses the RTCC (timer0) to time a
single pulse ////
//// input to the PIC.
////
////
////
////
////
//// (C) Copyright 1996,1997 Custom Computer
Services ////
////
////
/////////////////////////////////////////////////////
////////////////////
#include <16F84.H>
#fuses XT,NOPROTECT,NOWDT
#include
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2)
char get_scale() {
char scale;
do {
printf("\n\rPress S for short or L for long: ");
scale = getc();
scale = toupper(scale);
} while ( (scale!='S') && (scale!='L') );
return(scale);
}
void wait_for_low_to_high() {
void wait_for_low() {
delay_us(3); /* account for rise
time */
main() {
char scale;
byte time;
do {
scale = get_scale();
if(scale=='S')
setup_counters( RTCC_INTERNAL,
RTCC_DIV_64 );
else
setup_counters( RTCC_INTERNAL,
RTCC_DIV_256 );
printf("\n\rWaiting...\n\r");
wait_for_low_to_high();
set_rtcc(0);
wait_for_low();
time = get_rtcc();
} while (TRUE);
}
Library for Dallas 1621 Temperature
chip
/////////////////////////////////////////////////////
//////////////////////
////
////
//// Library for a Dallas 1621 Temperature chip
////
////
////
//// init_temp(); Call before the other
functions are used ////
////
////
//// d = read_temp(); Read the temerature in
degrees (0-255) ////
////
////
////
////
//// (C) Copyright 1996,1997 Custom Computer
Services ////
////
////
/////////////////////////////////////////////////////
//////////////////////
i2c_start();
i2c_write(0x90);
i2c_write(0xac);
i2c_write(data);
i2c_stop();
delay_ms(11);
}
void init_temp() {
output_high(PIN_B7);
output_high(PIN_B6);
i2c_start();
i2c_write(0x90);
i2c_write(0xee);
i2c_stop();
temp_config(8);
}
i2c_start();
i2c_write(0x90);
i2c_write(0xaa);
i2c_start();
i2c_write(0x91);
datah=i2c_read();
datal=i2c_read(0);
i2c_stop();
data=datah;
data=data*9;
if((datal&0x80)!=0)
data=data+4;
data=(data/5)+32;
datal=data;
return(datal);
}
Example to Read temperature using
the DS1621
/////////////////////////////////////////////////////
//////////////////////
////
////
//// EX_TEMP.C
////
////
////
//// Reads temperature using the DS1621 and sends
it over the RS232 ////
////
////
//// (C) Copyright 1996,1997 Custom Computer
Services ////
////
////
/////////////////////////////////////////////////////
//////////////////////
#include <16F84.H>
#use delay(clock=4000000)
#include
main() {
byte value;
init_temp();
do {
value = read_temp();
printf("%u\r\n",value);
delay_ms(1000);
}while (TRUE);
}
#IFNDEF EXP_OUT_ENABLE
output_low(EXP_OUT_CLOCK);
output_low(EXP_OUT_ENABLE);
#IFNDEF EXP_IN_ENABLE
#ENDIF
output_high(EXP_IN_CLOCK);
output_low(EXP_IN_ENABLE); // Latch all inputs
output_high(EXP_IN_ENABLE);
for(i=1;i<=NUMBER_OF_74165*8;++i) { // Clock
in bits to the ei structure
shift_left(ei,NUMBER_OF_74165,input(EXP_IN_DI));
output_low(EXP_IN_CLOCK);
output_high(EXP_IN_CLOCK);
}
output_low(EXP_IN_ENABLE);
}
#include <74595.C>
#include <74165.C>
main() {
byte data;
do {
read_expanded_inputs (&data);
//toggled
write_expanded_outputs (&data);
} while (TRUE);
}
TW523 X10 Driver
/////////////////////////////////////////////////////
/////////////////////
//// TW523 X10 Driver
////
////
////
//// x10_write(house_code,key_code) Send a data
burst, house_code ////
//// must be 'A'
to 'P' and ////
//// key_code is
0-1F ////
////
////
//// x10_read( house_code, key_code) Waits for and
reads the next ////
//// data burst.
////
////
////
//// x10_data_ready() Returns true
if a data burst ////
//// is starting.
Be sure to call ////
//// faster than
1khz in order not ////
//// to miss any
data. ////
//// Connect B0 to TW523 pin 1
////
//// B1 3
////
//// B2 4
////
//// GND 2
////
////
////
//// (C) Copyright 1996,1997 Custom Computer
Services ////
////
////
/////////////////////////////////////////////////////
/////////////////////
#ifndef X10_ZERO_CROSS
#endif
'F','G','H','K','L','I','J'};
byte const X10_KEY_CODES[16] =
{13,14,15,16,3,4,1,2,5,6,7,8,11,12,9,10};
void wait_for_zero_cross() {
if(input(X10_ZERO_CROSS))
while(input(X10_ZERO_CROSS)) ;
else
while(!input(X10_ZERO_CROSS)) ;
}
for(i=1;i<=n;++i) {
wait_for_zero_cross();
the_bit=shift_right(&data,1,0);
output_bit(X10_FROM_PIC, the_bit);
delay_ms(1);
output_low(X10_FROM_PIC);
if(start==0) {
wait_for_zero_cross();
output_bit(X10_FROM_PIC, !the_bit);
delay_ms(1);
output_low(X10_FROM_PIC);
}
}
}
i=0;
while (X10_HOUSE_CODES[i]!=house_code)
i++;
house_code=i;
if(key_code<16) {
i=0;
while (X10_KEY_CODES[i]!=key_code)
i++;
key_code=i;
}
x10_write_bits(7,4,1);
x10_write_bits(house_code,4,0);
x10_write_bits(key_code,5,0);
x10_write_bits(0,6,1);
}
byte x10_data_ready() {
port_b_pullups(TRUE);
return(!input(X10_TO_PIC));
}
byte x10_read_bits(byte n) {
byte data,i;
for(i=1;i<=n;++i) {
wait_for_zero_cross();
delay_us(300);
shift_right(&data,1,input(X10_TO_PIC));
wait_for_zero_cross();
delay_us(300);
}
data>>=8-n;
return(data);
}
port_b_pullups(TRUE);
x10_read_bits(2);
*house_code=x10_read_bits(4);
*house_code=X10_HOUSE_CODES[*house_code];
*key_code=x10_read_bits(5);
if(*key_code<16)
*key_code=X10_KEY_CODES[*key_code];
}
#include <16F84.H>
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2)
#fuses XT,NOPROTECT,NOWDT
main() {
char house_code;
byte key_code;
printf("Online\n\r");
while (TRUE) {
if(kbhit()) {
house_code = getc();
if((house_code>='A') && (house_code<='P')) {
putc(house_code);
key_code=gethex();
x10_write(house_code,key_code);
x10_write(house_code,key_code);
}
}
if(x10_data_ready()) {
putc('>');
x10_read(&house_code,&key_code);
printf("%c%2X",house_code,key_code);
}
}
}
byte gethex1() {
char digit;
digit = getch();
putchar(digit);
if(digit<='9')
return(digit-'0');
else
return((toupper(digit)-'A')+10);
}
byte gethex() {
int lo,hi;
hi = gethex1();
lo = gethex1();
if(lo==0xdd)
return(hi);
else
return( hi*16+lo );
}
max--;
len=0;
do {
c=getc();
if(c==8) { // Backspace
if(len>0) {
len--;
putc(c);
putc(' ');
putc(c);
}
} else if ((c>=' ')&&(c<='~'))
if( len < max ) {
s[len++]=c;
putc(c);
}
} while(c!=13);
s[len]=0;
}
#ifdef _stdlib_
get_string(s, 5);
i=atoi(s);
return(i);
}
signed long get_long() {
char s[7];
signed long l;
get_string(s, 7);
l=atol(s);
return(l);
}
#endif