#include "mcc_generated_files/mcc.h"
#include <stdio.h> // neccesary to printf to uart
#include <stdlib.h> // necessary to printf to uart
// 変数定義
uint16_t Temperature, Humidity;
char DataByte[5], Flag;
char Line1[17] = "Temp = xx.x DegC";
char Line2[17] = "Humi = xx.x %RH ";
// 関数プロト
void GetSensor(void);
/******************************
* タイマ0割り込み処理関数
* 2秒周期
*****************************/
void TMR0_Process(void){
Flag = 1;
}
// printf()出力をUARTに流す
void putch(char data)
{
EUSART_Write(data);
}
void main(void)
{
SYSTEM_Initialize();
SYSTEM_Initialize();
// タイマ0 Callback関数定義
TMR0_SetInterruptHandler(TMR0_Process);
// 割り込み許可
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
/**** メインループ ******************/
while (1)
{
if(Flag == 1){ // 2秒ごと
// LED_SetHigh(); // 目印
Flag = 0;
// センサデータ取得 2項目
GetSensor();
Humidity = (uint16_t)DataByte[0]*256 + DataByte[1];
Temperature = (uint16_t)DataByte[2]*256 + DataByte[3];
// 文字に変換しバッファに格納
Line1[7] = (char)(Temperature / 100) + '0';
Temperature %= 100;
Line1[8] = (char)(Temperature /10) + '0';
Line1[10] = (char)(Temperature % 10) + '0';
Line2[7] = (char)(Humidity / 100) + '0';
Humidity %= 100;
Line2[8] = (char)(Humidity / 10) + '0';
Line2[10] = (char)(Humidity % 10) + '0';
printf("temperature is %s\n",Line1);
printf("humidity is %s\n", Line2);
//LED_SetLow();
}
}
// // 確認用LED点灯(RA0などにLEDをつけている場合)
// TRISA0 = 0; // 出力
// LATA0 = 1;
//
// printf("\r\n=== UARTテスト開始 ===\r\n");
//while (1)
// {
// printf("Hello from PIC16F18313!\r\n");
// __delay_ms(1000);
// }
}
/*******************************************
* AM2320 Get Data
* 5Byte HumiH+HumiL+TempH+TempL+Check
********************************************/
void GetSensor(void){
uint8_t i, j, pos;
// Output StartPulse
SDA_SetLow();
SDA_TRIS = 0; // 出力モード
__delay_ms(2); // 2msec
SDA_SetHigh();
SDA_TRIS = 1; // 入力モード
__delay_us(150); // 最初のパルススキップ
while(SDA_GetValue() == 1); // Highの間待つ
// Get 40bit data 5 byte
for(j=0; j<5; j++){ // 5バイト繰り返し
DataByte[j] = 0; // データリセット
pos = 0x80; // 最上位ビットから
for(i=0; i<8; i++){ // 8ビット繰り返し
while(SDA_GetValue() == 0); // Lowの50usec待ち
__delay_us(40); // 40usecスキップ
if(SDA_GetValue() == 1){ // 0と1の判定
DataByte[j] |= pos; // 1の場合セット
while(SDA_GetValue() == 1); // ビット終わりを待つ
}
pos >>= 1; // 次のビットへ
}
}
}
0 件のコメント:
コメントを投稿