/***********************************************************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products.
* No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
* applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIESREGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY
* LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR
* ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability
* of this software. By using this software, you agree to the additional terms and conditions found by accessing the
* following link:
*
http://www.renesas.com/disclaimer
*
* Copyright (C) 2012, 2014 Renesas Electronics Corporation. All rights reserved.
***********************************************************************************************************************/
/***********************************************************************************************************************
* File Name : r_cg_main.c
* Version : Code Generator for RL78/R7F0C8021 V1.02.01.01 [30 May 2014]
* Device(s) : R7F0C8021
* Tool-Chain : CA78K0R
* Description : This file implements main function.
* Creation Date: 2014-9-21
***********************************************************************************************************************/
/***********************************************************************************************************************
Pragma directive
***********************************************************************************************************************/
/* Start user code for pragma. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */
/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "r_cg_macrodriver.h"
#include "r_cg_cgc.h"
#include "r_cg_port.h"
#include "r_cg_wdt.h"
#include "r_cg_sau.h"
/* Start user code for include. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */
#include "r_cg_userdefine.h"
/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
/* Start user code for global. Do not edit comment generated here */
unsigned short temp_data;
uint8_t tx_buf[] = {"Temperature is "};
uint8_t temp_buf[3] = {0x32,0x33,0x43};
uint8_t end_buf[2] = {"\n"};
/* End user code. Do not edit comment generated here */
void R_MAIN_UserInit(void);
void send_data(void);
void time_delay(unsigned int count);
void delay(unsigned short time);
unsigned char DS18B20_INIT();
unsigned char DS18B20_READ();
void DS18B20_WRITE(unsigned char data);
void DS18B20_SENDCHANGECMD();
void DS18B20_SENDREADCMD();
unsigned short DS18B20_GETTEMP();
/***********************************************************************************************************************
* Function Name: main
* Description : This function implements main function.
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void main(void)
{
uint8_t rx_buf[2];
uint8_t i;
R_MAIN_UserInit();
/* Start user code. Do not edit comment generated here */
while (1U)
{
R_UART0_Receive(rx_buf, 1);
switch(rx_buf[0])
{
case 0x11:
P0.4 = 0;
rx_buf[0] = 0x00;
break;
case 0x12:
P0.4 = 1;
rx_buf[0] = 0x00;
break;
case 0x13:
for(i=0;i<20;i++)
{
P0.4 = ~P0.4;
time_delay(200);
}
rx_buf[0] = 0x00;
break;
case 0x14:
//DS18B20_SENDCHANGECMD();
//temp_data=DS18B20_GETTEMP();
//time_delay(2000);
//temp_data = temp_data * 63;
//temp_buf[0] = (unsigned char)(temp_data>>8);
//temp_buf[1] = (unsigned char)temp_data;
temp_buf[0] = 0x33;
temp_buf[1] = 0x34;
R_UART0_Send(tx_buf, sizeof(tx_buf));
time_delay(100);
R_UART0_Send(temp_buf, sizeof(temp_buf));
time_delay(100);
R_UART0_Send(end_buf, sizeof(end_buf));
time_delay(100);
rx_buf[0] = 0x00;
break;
case 0x15:
rx_buf[0] = 0x00;
break;
default:
break;
}
}
/* End user code. Do not edit comment generated here */
}
/***********************************************************************************************************************
* Function Name: R_MAIN_UserInit
* Description : This function adds user code before implementing main function.
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void R_MAIN_UserInit(void)
{
/* Start user code. Do not edit comment generated here */
EI();
R_UART0_Start();
/* End user code. Do not edit comment generated here */
}
void delay(unsigned short time) //delay for timex us.
{
while(--time);
}
unsigned char DS18B20_INIT()
{
unsigned char data;
unsigned char res;
R_PORT_Create_OUT();
P0.2=1;
delay(10);
P0.2=0;
delay(960);//480.35us
R_PORT_Create_IN();
delay(131);//66.35us
data=P0.2;
delay(829);//480-66us
if(data==0)
{
res=1;
}
else
{
res=0;
}
return res;
}
unsigned char DS18B20_READ()
{
unsigned char data;
unsigned char i;
for(i=8;i>0;i--)
{
data>>=1;
R_PORT_Create_OUT();
P0.2=0;
delay(11);//6.35us
P0.2=1;
delay(7);//4.35us
R_PORT_Create_IN();
if(P0.2)
{
data|=0x80;
}
delay(125);//25us
}
delay(1);
R_PORT_Create_OUT();
return data;
}
void DS18B20_WRITE(unsigned char data)
{
unsigned char i;
unsigned char temp;
delay(10);
R_PORT_Create_OUT();
for(i=8;i>0;i--)
{
temp=data&0x01;
P0.2=0;
delay(2);////////////////////
if(temp==1) {P0.2=1;}
delay(125);//63.35us
P0.2=1;
NOP();
NOP();
data=(data>>1);
}
}
void DS18B20_SENDCHANGECMD()
{
DS18B20_INIT();
delay(1);
DS18B20_WRITE(0xcc);
DS18B20_WRITE(0x44);
}
void DS18B20_SENDREADCMD()
{
DS18B20_INIT();
delay(1);
DS18B20_WRITE(0xCC);
DS18B20_WRITE(0xbe);
}
unsigned short DS18B20_GETTEMP()
{
unsigned short value;
//float t;
unsigned char low,high;
DS18B20_SENDREADCMD();
low=DS18B20_READ();
high=DS18B20_READ();
value=(unsigned short)high;
value=(value<<8)|low;
//t=value*0.0625;
//value=t*10+0.5;
return value;
}
void time_delay(unsigned int count)
{
unsigned int i,j;
for(i=0; i
for(j=0; j<500; j++);
}
/* Start user code for adding. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */