nobcha23の日記

PICマイコンやArduinoを使う電子回路遊びを紹介します

mainは次

温度計表示のMAINです。

/*************************************************
 MSSP i2c THERMOMETER of PIC12F1822
              By nobcha all right reserved

  Ver 1. 07/04/2013 for stts751 & AQM0802A

   PIC12F1822 
   PIN Assign 	#7 RA0:monitor LED
   		#6 RA1:SCL
		#5 RA2:SDA

   OSC INT 8MHz
   Development Circumstance
   MPLAB IDE V8.73   HiTECH C V9.83
************************************************* */
#define _LEGACY_HEADERS
#define _XTAL_FREQ 8000000

#include <htc.h>

#include "lcd_i2c_mssp.h"
#include "i2c_stts.h"

__CONFIG(	
    FOSC_INTOSC & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF 
    & CPD_OFF & BOREN_OFF & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF
	);

__CONFIG(
	WRT_OFF & PLLEN_OFF & STVREN_ON &  LVP_OFF
	);

// void itostring(char digit, unsigned int data, char *buffer);
void mssp_init(void);

unsigned char Msg1[8] = "THERMO";

void main(void)
{

	unsigned char thermo[2], stts_sts=0;

	/* INITIALIZE REGISTER      */
	OSCCON = 0b01110000;		// Set  8MHz
	PORTA = 0b00000000;		// Clear
	TRISA = 0b00000110;		// RA1,RA2 INPUT
	ANSELA = 0b00000000;		// All digital
	CM1CON0 = 0b00000111;		// No using compalator

	mssp_init();			// MSSP initialize
	RA0 = 1;
	lcd_init();			// LCD initialize

	stts_config(0x3 );		// 12bit rez:0b11
	stts_conv(5 );			// 2 times per second operation

	while(1)
	{
		__delay_ms(200);

		LATA0 ^= 1;		// Heart beat
		lcd_cmd(0x80);		// Move cursor 1st line
		lcd_str(Msg1);		// Display tytle message

		while( stts_busy(stts_sts));
		stts_read(thermo);
		lcd_goto(0x40);		// go to the top of 2nd line
		lcd_data((thermo[0]&0x80)?'-':'+');
		thermo[0]=thermo[0]&0x7F;

		if(thermo[0]>=100)
		{
			lcd_data((thermo[0]/100)|0x30);
			thermo[0]=(thermo[0]%100);
		}

		if(thermo[0]>=10)
		{
			lcd_data((thermo[0]/10)|0x30);
			thermo[0]=(thermo[0]%10);
		}

		lcd_data(thermo[0]|0x30);
	    lcd_data('.');

		thermo[1]=(char)((float)(thermo[1]>>4)*6.25);
		lcd_data(thermo[1]/10|0x30);
		
		lcd_data((thermo[1]%10)|0x30);
					 
	    lcd_data('゚');
	    lcd_data('C');

	}
}

/***************************************
* MSSP initialize
****************************************/
void mssp_init(void){ 
       /* SSP1CON1 REGISTERS */
       SSPEN      = 1;   		//Enables Serial Port Mode
       SSPM3      = 1;   		/////////
       SSPM2      = 0;   		//I2C Master Mode
       SSPM1      = 0;   		// clock= Fosc/(4*(SSP1ADD+1))
       SSPM0      = 0;   		////////

       /* SSPCON2 REGISTERS */
       SSP1CON2   = 0x00;
       /* SSPCON3 REGISTERS */
       SSP1CON3   = 0x00;


       /* SSP1STAT REGISTERS */
       SMP       = 1;   		//SPI MASTER MODE
       CKE       = 1;   		//SMBus Specific Inputs Enabled

       //SSP1ADD    = 0x19;    	//~75kHz
       //SSP1ADD    = 0x13;    	//~100kHz
       //SSP1ADD    = 0x07;     //~400kHz
       SSP1ADD    = 0x50;
}