back to the topic, I found my old project for my hakko driver (I have hakko pencils available here so I made a driver) ... you seen the picture .. as I said I use 16F688 (no idea why I decided to do so) and I read thermocouple from pencil as NTC (or PTC I don't remember really) :D (it actually works!!!) ... here's the code .. it is very simple, written in ccs c (I have some fairly old version I purchased with some DLP module few years ago, works with 16F and smaller pic's and unfortunately cannot be upgraded for free :( ) .. I never wrote the l2t function (convert ADC value to C):
// display the bar while changing the "target temp" in loops
#define BAR_DELAY 100
#define HEATER_ON() output_high(PIN_A4)
#define HEATER_OFF() output_low(PIN_A4)
#define BUTTON_UP PIN_A1
#define BUTTON_DOWN PIN_A2
#define BUTTON_F PIN_A3
#include <16F688.h>
#device adc=10
#FUSES WDT //Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOPUT //No Power Up Timer
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#use delay(clock=8000000,RESTART_WDT)
unsigned long temperature; //0-1023 ZELJENA TEMPERATURA
unsigned long current;
unsigned int f, i, j;
unsigned int DEBOUNCE_TIME;
unsigned int HYST;
// LCD.RS == PIN_A5
struct lcd_pin_map {
int data : 4;
BOOLEAN enable;
BOOLEAN rw;
BOOLEAN nc1;
BOOLEAN nc2;
} lcd;
#locate lcd = getenv("sfr:PORTC")
#define set_tris_lcd(x) set_tris_c(x)
#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40 // LCD RAM address for the second line
BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
// These bytes need to be sent to the LCD
// to start it up.
// The following are used for setting
// the I/O port direction register.
struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out
struct lcd_pin_map const LCD_READ = {15,0,0,0,0}; // For read mode data pins are in
BYTE lcd_read_byte() {
BYTE low,high;
set_tris_lcd(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_lcd(LCD_WRITE);
return( (high<<4) | low);
}
void lcd_send_nibble( BYTE n ) {
lcd.data = n;
delay_cycles(1);
lcd.enable = 1;
delay_us(2);
lcd.enable = 0;
}
void lcd_send_byte( BYTE address, BYTE n ) {
output_low(PIN_A5); // lcd.rs = 0;
while ( bit_test(lcd_read_byte(),7) ) ;
output_bit(PIN_A5, address); //lcd.rs = address;
delay_cycles(1);
lcd.rw = 0;
delay_cycles(1);
lcd.enable = 0;
lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
}
void lcd_init() {
BYTE i;
set_tris_lcd(LCD_WRITE);
output_low(PIN_A5); // 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]);
}
void lcd_gotoxy( BYTE x, BYTE y) {
BYTE address;
if(y!=1)
address=lcd_line_two;
else
address=0;
address+=x-1;
lcd_send_byte(0,0x80|address);
}
void lcd_putc( char c) {
switch (c) {
case 'f' : lcd_send_byte(0,1);
delay_ms(2);
break;
case 'n' : lcd_gotoxy(1,2); break;
case 'b' : lcd_send_byte(0,0x10); break;
default : lcd_send_byte(1,c); break;
}
}
//1022 - room temp
//
unsigned long l2t (unsigned long a){ // get the temp in C from 0-1023
return (a*10+433)/3; //return C*10
}
void store(unsigned long x){
// SAVE TO EEPROM
write_eeprom(0, (unsigned int8) (x >> 8) & 0xff);
write_eeprom(1, (unsigned int8) x & 0xff);
}
unsigned long fetch(){
// FETCH FROM EEPROM
unsigned long x;
x = read_eeprom(0);
x = x << 8;
x = x + read_eeprom(1);
return x;
}
void menu(){
unsigned long temp;
temp = temperature;
temperature = fetch();
//turn off the heater
HEATER_OFF();
// DEFAULT TEMPERATURE
printf(lcd_putc, "f- menu +n ENTER ");
while (!input(BUTTON_F)) restart_wdt(); // wait for Function key to continue
delay_ms(DEBOUNCE_TIME);
while (input(BUTTON_F)) restart_wdt(); // wait for release
printf(lcd_putc, "f- %4Lu +n Power On TEMP ", l2t(temperature)/10);
while (!input(BUTTON_F)){
if ( input(BUTTON_UP) ){ // button pressed
delay_ms(DEBOUNCE_TIME); //debounce
if ( input(BUTTON_UP) ){
if (temperature < 1023) ++temperature;
}
}//BUTTON UP
if ( input(BUTTON_DOWN) ){ // button pressed
delay_ms(DEBOUNCE_TIME); //debounce
if ( input(BUTTON_DOWN) ){
if (temperature > 0) --temperature;
}
}//BUTTON DOWN
lcd_gotoxy(6,1);
printf(lcd_putc, "%4Lu", l2t(temperature)/10);
}
while (input(BUTTON_F)) restart_wdt(); // wait for release
//STORE VALUE
store(temperature);
temperature = temp;
// DEFAULT DEBOUNCE TIME
printf(lcd_putc, "f- %4u +n DEBOUNCE (ms) ", DEBOUNCE_TIME);
while (!input(BUTTON_F)){
if ( input(BUTTON_UP) ){ // button pressed
delay_ms(DEBOUNCE_TIME); //debounce
if ( input(BUTTON_UP) ){
if (DEBOUNCE_TIME < 250) ++DEBOUNCE_TIME;
}
}//BUTTON UP
if ( input(BUTTON_DOWN) ){ // button pressed
delay_ms(DEBOUNCE_TIME); //debounce
if ( input(BUTTON_DOWN) ){
if (DEBOUNCE_TIME > 5) --DEBOUNCE_TIME;
}
}//BUTTON DOWN
lcd_gotoxy(6,1);
printf(lcd_putc, "%4u", DEBOUNCE_TIME);
}
while (input(BUTTON_F)) restart_wdt(); // wait for release
//STORE VALUE
write_eeprom(2, DEBOUNCE_TIME);
// DEFAULT HYSTERESIS
printf(lcd_putc, "f- %4u +n HYSTERESIS ", HYST);
while (!input(BUTTON_F)){
if ( input(BUTTON_UP) ){ // button pressed
delay_ms(DEBOUNCE_TIME); //debounce
if ( input(BUTTON_UP) ){
if (HYST < 250) ++HYST;
}
}//BUTTON UP
if ( input(BUTTON_DOWN) ){ // button pressed
delay_ms(DEBOUNCE_TIME); //debounce
if ( input(BUTTON_DOWN) ){
if (HYST > 2) --HYST;
}
}//BUTTON DOWN
lcd_gotoxy(6,1);
printf(lcd_putc, "%4u", HYST);
}
while (input(BUTTON_F)) restart_wdt(); // wait for release
//STORE VALUE
write_eeprom(3, HYST);
}
void main()
{
setup_adc_ports(sAN0|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
setup_timer_0(RTCC_INTERNAL);
setup_wdt(WDT_DIV_65536);
setup_timer_1(T1_DISABLED);
setup_comparator(NC_NC);
setup_vref(FALSE);
setup_oscillator(OSC_8MHZ);
current = 0;
f = 0;
set_adc_channel(0);
lcd_init();
printf(lcd_putc, " CRSN DOO Lemilon V2.1");
temperature = fetch();
DEBOUNCE_TIME = read_eeprom(2);
if (DEBOUNCE_TIME < 5) DEBOUNCE_TIME = 5;
if (DEBOUNCE_TIME > 250) DEBOUNCE_TIME = 50;
HYST = read_eeprom(3);
if (HYST < 2) HYST = 2;
if (HYST > 250) HYST = 3;
delay_ms(1000);
lcd_putc('f'); //clear display
loop:
read_adc(ADC_START_ONLY); // start the conversion
if ( input(BUTTON_UP) ){ // button pressed
delay_ms(DEBOUNCE_TIME); //debounce
if ( input(BUTTON_UP) ){
if (temperature < 1023) ++temperature;
f = BAR_DELAY;
}
}//BUTTON UP
if ( input(BUTTON_DOWN) ){ // button pressed
delay_ms(DEBOUNCE_TIME); //debounce
if ( input(BUTTON_DOWN) ){
if (temperature > 0) --temperature;
f = BAR_DELAY;
}
}//BUTTON DOWN
if ( input(BUTTON_F) ){ // button pressed
delay_ms(DEBOUNCE_TIME); //debounce
if ( input(BUTTON_F) ){
while (input(BUTTON_F)) restart_wdt(); // wait for release
while(input(BUTTON_F)); //wait for release
menu();
}
}//BUTTON Function
current = (current + read_adc(ADC_READ_ONLY)) >> 1; //read the data
if (f == 0){
lcd_gotoxy(1,1);
printf(lcd_putc, "Current: %4Lu.%1i ", l2t(current)/10, (int8) l2t(current)%10);
lcd_gotoxy(1,2);
printf(lcd_putc, "Target : %4Lu ", l2t(temperature)/10);
} else {
lcd_gotoxy(1,1);
j = temperature >> 6;
for (i = 0; i < j; ++i){
lcd_putc('#');
}
for (i = j; i < 15; ++i){
lcd_putc('.');
}
lcd_gotoxy(1,2);
printf(lcd_putc, "Target : %4Lu.%1i ", l2t(temperature)/10, (int8) l2t(temperature)%10);
}
if (current < temperature - HYST){
HEATER_ON();
}else if (current > temperature){
HEATER_OFF();
}
if (f > 0) --f;
goto loop;
}