|
楼主 |
发表于 2013-4-23 07:08:07
|
显示全部楼层
Here is the finished design.
V1 is the temperature sensor (a thermocouple) in the handle. Heater is the handle's heating element.
I used NE5532 as the thermocouple amplifier - the schematic used TL082 - which incidentally doesn't work in the 5v environment.
Gain control is implemented by replacing R7 with a 1k resistor + 1k pot, making gain adjustment from 50x - 100x.
Code:
- //code to control an atten907 /4-wire heater (thermocouple temperature sensor)
- //with a pot (to control temperature)
- //Host: 12F675. Minimalist implementation: No LCD, no C2F, ...
- //version history for 16F684
- //4/07/2013: v0.90 - initial code adapted for 16F684
- //code to control an atten907 938/4-wire heater
- //with a pot (to control temperature)
- //version history
- //4/20/2013: v0.90 - initial code integrated
- //4/22/2013: v1.00 - 1st hot-tested. Success!
- #include <htc.h>
- #include <string.h> //we use strcpy
- #include "config.h" //internal rc oscillator, no clock out
- #include "gpio.h"
- #include "delay.h"
- #include "adc.h"
- //#include "lcd_4bit.h" //we use lcd4bit
- //#include "misc.h" //we use ultoa
- //for compatability for 12F675
- #define PORTA GPIO
- #define TRISA TRISIO
- //hardware configuration
- #define HEATER_PORT PORTA //turn on / off the heaterP/N
- #define HEATER_DDR TRISA
- #define HEATER (1<<1) //heater Positive - active high
- #define SENSOR_PORT PORTA //temperature sensor
- #define SENSOR_DDR TRISA
- #define SENSOR (1<<0) //temperature sensor on pa0
- #define SENSOR_ADC ADC_AN0 //pa.0 = an0
- #define Vref 5000ul //reference voltage for the adc
- //thermocouple output: 0mv at 25c and 20mv at 400c
- //connection: thermocouple -> thermocouple amplifier -> adc
- //resistance curve
- #define Temp_Low 25 //low temp, in celsius
- #define Temp_High 400 //high temp, in celsius
- //a1321 sensor curve
- #define mv_at_Low 5 //ptc resistance (in ohm) at Temp_Low
- #define mv_at_High 2000 //ptc resistance (in ohm) at Temp_High
- #define ADC2mv(adc) ((unsigned long) ((adc) * Vref / 1024) //convert 10-bit adc to Ohm readings
- #define mv2Temp(mv) (((mv)<mv_at_Low)?Temp_Low:(Temp_Low + ((unsigned long)(mv) - mv_at_Low) * (Temp_High - Temp_Low) / (mv_at_High - mv_at_Low)))
- #define TARGET_PORT PORTA //pot to set target temperature
- #define TARGET_DDR TRISA
- #define TARGET (1<<2) //target temperature set on pa2
- #define TARGET_ADC ADC_AN2 //pa.2=an2
- #define TARGET_MIN 200 //minimum target temperature, in C (adc reading. top 6 bits
- #define TARGET_MAX 480 //maximum target temperature, in C (adc reading. top 6 bits
- //specify led indicators
- //we use two leds
- //green led - on = target temperature has been reached
- //red led - on = heater on (target temperature has not been reached)
- #define LED_PORT PORTA //port for green led indicators
- #define LED_DDR TRISA
- #define LEDG (1<<4) //green led indicator. comment out if not used
- #define LEDR (1<<5) //red led indicator. comment out if not used
- #define LOOP_PR 100 //loop period, in ms
- #define LCD_DEGREEC 1 //lcd_degree char
- #define ADC_MASK 0x3f //adc mask. 6 bits - not used
- //end hardware configuration
- #define HEATER_ON(pins) do {NOP4(); IO_SET(HEATER_PORT, pins); } while (0) //turn on heater. active low
- #define HEATER_OFF(pins) do {NOP4(); IO_CLR(HEATER_PORT, pins); } while (0) //turn on heater, active high
- #define LED_ON(pins) IO_SET(LED_PORT, pins) //turn on led, active low
- #define LED_OFF(pins) IO_CLR(LED_PORT, pins) //turn off led, active high
- //celsius / fahrenheit selector
- #define C2F_PORT PORTA
- #define C2F_DDR TRISA
- #define C2F_PIN (1<<3) //on pa3 - 0 = fahrenheit. 1 = celsius (with a pull-up)
- #define C2F(degree_c) (9 * (degree_c) / 5 + 32) //convert celsius to fahrenheit
- //global variables
- unsigned char loop_cnt=0; //loop counter
- unsigned short tmp_adc, tmp=0; //temperature sensor
- unsigned short tgt_adc, tgt=0;
- //unsigned char lcd_displayC; //display in C indicator = 1 = displa in Celsius. 0=Fahrenheit
- #if 0 //no lcd display
- //for the lcd - lcd16x2
- unsigned char vRAM[17]; //display ram
- const unsigned char str0[]="938 Ctrller v1.0";
- const unsigned char ver[] ="--version 1.0--";
- const unsigned char str1[]="TARGET= C ";
- const unsigned char str2[]="SENSOR= C ";
- const unsigned char str3[]="TGT= /TMP= ";
- const unsigned char lcd_degreeC[]={
- 0x07, 0x05, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00};
- const unsigned char lcd_up[]={
- //0x04, 0x0e, 0x1f, 0x04, 0x04, 0x04, 0x0a, 0x0f
- 0x04, 0x0e, 0x1f, 0x00, 0x1f, 0x00, 0x00, 0x00
- };
- #define LCD_UP 2 //0x01 is special charter
- #endif
-
- //initialize the controller
- void ctrl_init(void) {
- HEATER_OFF(HEATER); //turn off heater
- IO_OUT(HEATER_DDR, /*HEATERN | */HEATER); //heater as output
- LED_OFF(LEDG | LEDR); //turn off the leds
- IO_OUT(LED_DDR, LEDG | LEDR); //led as output
- IO_IN(SENSOR_DDR, SENSOR); //sensor as input
- IO_IN(TARGET_DDR, TARGET); //target as input
- IO_IN(C2F_DDR, C2F_PIN); //c2f selector as input
- adc_init(); //reset the adc module
-
- //load up lcd special chacterators
- //lcd_cgram(LCD_DEGREEC, lcd_degreeC); //load up the special char
- //lcd_cgram(LCD_UP, lcd_up); //load up the special char
- }
- //read solder sensors
- unsigned short read_sensor(void) {
- //unsigned short tmp; //tmp variable
- tmp_adc=adc_read(SENSOR_ADC); //read the temperature sensor
- return mv2Temp(ADC2mv(tmp_adc)); //lowest 6 bits
- //return ADC2Ohm(tmp);
- }
- //read the pot that sets the target temperature
- //adc 0 -> TARGET_MIN
- //adc 1023 -> TARGET_MAX
- unsigned short read_target(void) {
- //unsigned short tmp; //tmp variable
- //tgt_adc = adc_read(TARGET_ADC); //read the pot, if clock-wise turns increase tgt_adc
- tgt_adc = 1024-adc_read(TARGET_ADC); //read the pot, if clock-wise turns decrease tgt_adc
- return TARGET_MIN + (unsigned long) tgt_adc * (TARGET_MAX - TARGET_MIN) / 1024; //lowest 6 bits
- }
- //reset the chip
- void mcu_init(void) {
- //IRCF2=1, IRCF1=1, IRCF0=0; //running at 4Mhz
-
- ANSEL=0x00; //all pins PORTA
-
- //ANSELH=0x00;
- CMCON = 0x07; //turn off analog comparators
- //CM2CON0 = 0x00;
- }
- //adc the sensors
- void ctrl_adc(void) {
- //lcd_displayC = IO_GET(C2F_PORT, C2F_PIN); //read c2f_pin -> 0=celsius, 1=fehrenheit
- //if (lcd_displayC) { //pin is pulled high -> celsius
- tmp=read_sensor(); //read the pot / target temperature
- tgt=read_target(); //read temperature sensor,
- //} else { //pin is pulled low - fahrenheit
- //tmp=C2F(read_sensor()); //read the pot / target temperature
- //tgt=C2F(read_target()); //read temperature sensor,
- //}
- }
- //control the heater
- void ctrl_heater(void) {
- //temperature control logic
- if (tmp < tgt) {
- LED_ON(LEDR); NOP4(); LED_OFF(LEDG); //turn on the red led / off the green led
- HEATER_ON(HEATER); //turn on the heater
- } else {
- LED_OFF(LEDR); NOP4(); LED_ON(LEDG); //turn off the red led / on the green led
- HEATER_OFF(HEATER); //turn off the heater
- }
- }
- #if 0
- //display - 1line format
- void ctrl_display1(void) {
- //process line0
- strcpy(vRAM, str3);
- ultoa(&vRAM[4], tgt, 3);
- ultoa(&vRAM[13], tmp, 3);
- if (tgt > tmp) vRAM[7]=LCD_UP; //insert the up char
- lcd_display(LCD_Line0, vRAM);
- }
- //display - 2line format
- void ctrl_display2(void) {
- //process line0
- strcpy(vRAM, str1); vRAM[10]=LCD_DEGREEC;
- if(lcd_displayC == 0) vRAM[11]='F';
- ultoa(&vRAM[7], tgt, 3);
- ultoa(&vRAM[12], tgt_adc, 4);
- if (tgt > tmp) vRAM[6]=LCD_UP;
- lcd_display(LCD_Line0, vRAM);
-
- //process line1
- strcpy(vRAM, str2); vRAM[10]=LCD_DEGREEC;
- if (lcd_displayC == 0) vRAM[11]='F';
- ultoa(&vRAM[7], tmp, 3);
- ultoa(&vRAM[12], tmp_adc, 4);
- lcd_display(LCD_Line1, vRAM);
-
- }
- #endif
- void main(void)
- {
- mcu_init(); //reset the chip
- //lcd_init(); //reset the lcd
- ctrl_init(); //reset the controller
- //display sign-on message
- //strcpy(vRAM, str0); lcd_display(LCD_Line0, vRAM);
- //strcpy(vRAM, ver ); lcd_display(LCD_Line1, vRAM);
- delay_ms(1000);
- while (1){
- //TODO Auto-generated main function
- //read the tempeature info
- //HEATER_OFF(); //turn off the heater
- //read the sensors
- ctrl_adc();
-
- //control the heater
- ctrl_heater();
-
- //update display
- //ctrl_display1(); //one-line display
- //ctrl_display2(); //two-line display
-
- //waste some time
- delay_ms(LOOP_PR); //waste some time
- }
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|