⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 基于瑞萨 M16C 的最新版本 IIC 通信
💻 C
字号:
#include "sema.h"
#include "hardware.h"
#include "inthandler.h"
#include "uart.h"
#include "i2c.h"

typedef unsigned char		uint8;
typedef unsigned short	uint16;
typedef unsigned int		uint32;
typedef unsigned char		uchar;

float pot();
float temp();
void sleep(uint16 time);
void i2c();

void UART1_recv_ISR(void)
{
	uchar c = U1RB.BYTE.U1RBL;
	_printf("UART1 RECEIVE INTERRUPT: %c\n", c);
}

void SI03_INT4_ISR(void)
{
	asm("fclr i");
	//P2.BIT.P2_1 ^= HIGH;
	//_printf("TEMP: %f\n", temp());
	//_printf("POT: %f\n", pot());
	//sleep(1);
	_printf("INT4 interrupt!");
	i2c();
}

void TimerA0_ISR(void)
{
}

void TimerA1_ISR(void)
{
    P3.BIT.P3_7 ^= HIGH;
	P2.BIT.P2_0 ^= HIGH;
}



int main (void)
{

	PD2.BIT.PD2_0 = OUTPUT;				// setting port P20 as OUTPUT port
	P2.BIT.P2_0 = LOW;					// set LED on port P20 OFF initially
	
	PD2.BIT.PD2_1 = OUTPUT;				// setting port P20 as OUTPUT port
	
	PD3.BIT.PD3_7 = OUTPUT;				// setting port P37 as OUTPUT port
	P3.BIT.P3_7 = LOW;					// set LED on port P37 OFF initially
	
	/* interrupt 4 initiation */
	IFSR.BIT.IFSR6 = 1;					// enable interrupt4
	IFSR.BIT.IFSR4 = 0;					// One edge interrupt
	PD1.BIT.PD1_6 = INPUT;				// set port P16 (=INT4) as INPUT port
	INT4IC.BYTE = 0x03;					// enable interrupt4, level 3, falling edge
	
	/* initialise and start timer A0 */
	UDF = 0;							// down count (for all timers)
	TA0MR.BYTE = 0x80;					// timer mode, f/32
	TA0 = 62500;						// preload timer value 62500, 62500/(CPU_SPEED / 32) =  200.0 ms
	TABSR.BIT.TA0S = 1;					// start the timer

	/* initialise and start timer A1 with interrupt */
	TA1MR.BYTE = 0x01;					// event mode
	TRGSR.BYTE = 0x02; 					// count on timer A0 overflows
	TA1 = 0x04;							// count to 4
	//TA1IC.BYTE = 0x02;					// set TA1 interrupt priority level 2
	TABSR.BIT.TA1S = 1;					// start the timer
	
	/* initialise and start timer A2 for use in sleep function */
	TA1MR.BYTE = 0x01;					// timer mode, f/8 this gives a min of 800ns
	TA2 = 1250;							// count to 1250 equals to 1 ms
	TABSR.BIT.TA2S = 0;					// do not start the timer
	
	/* uart1 initiation */
	PD6.BIT.PD6_6 = INPUT;				// set port P66 to INPUT (RXD1)
	PD6.BIT.PD6_7 = OUTPUT;				// set port P67 to OUTPUT (TXD1)
	U1BRG = ((MAIN_CLOCK / (16.0 * 9600)) - 1.0);	// set UART1 bit rate generator
	U1MR.BYTE = 0x05;					// 8 data bits
	U1C0.BYTE = 0x10;					// select f1, CTS/RTS function disabled
	U1C1.BIT.TE = 1;					// enable transmit
    U1C1.BIT.RE = 1;					// enable receive
	S1RIC.BYTE = 0x04;					// set UART1 receive interrupt priority level 4
	
	/* adc initiation (AD0) */
	PD7.BIT.PD7_6 = OUTPUT;				// Enable the AVCC on MULLE
	P7.BIT.P7_6 = HIGH;					// - || -
	PD3.BIT.PD3_1 = OUTPUT;				// Enable the VREF on MULLE
	P3.BIT.P3_1 = HIGH;					// - || -
	PD10.BIT.PD10_0 = INPUT;			// Set AN0 as input
	PD0.BIT.PD0_7 = INPUT;				// Set AN17 as input
	ADCON0.BYTE = 0x00;					// Choose the AN0 as ADC input pin (default)
	ADCON1.BIT.BITS = HIGH;				// 10 bit mode
	ADCON1.BIT.VCUT = HIGH;				// Vref enable
	ADCON2.BYTE = 0x01;					// Use sample and hold and AD group P10 (default)
	
	/* temp sensor on mulle */
	PD3.BIT.PD3_0 = OUTPUT;				// enable temp sensor
	P3.BIT.P3_0 = LOW;					// - || -
	PD0.BIT.PD0_7 = INPUT;				// set AN17 as temp input

	InitI2C();





	asm("fset i");			    	/* enable interrupts */



	
	
	




	_printf("Initialisation of MULLE done!\n\n");





	/* main loop */

	for (;;) {
	}
	
	return 0;
}

void i2c() {
	StartI2C();
	//WriteI2C(0xA2);
	//IdleI2C();
	/*if (WriteI2C(0x00) == -1)
		return;
	IdleI2C();
	RestartI2C();
	IdleI2C();
	if (WriteI2C(0xA3) == -1)
		return;
	IdleI2C();
	while (ReadI2C() == -1);
	_printf("Read1: %d\n", ReadI2C());
	IdleI2C();
	AckI2C();
	IdleI2C();
	while (ReadI2C() == -1);
	_printf("Read2: %d\n", ReadI2C());
	IdleI2C();
	NackI2C();
	IdleI2C();
	StopI2C();*/
}


/* Vref have to be change to VCC for this to work */
float pot() {
	ADCON0.BYTE = 0x00; // Choose the AN0 as ADC input pin
	ADCON2.BYTE = 0x01;	// Use sample and hold and AD group P10

	ADCON0.BIT.ADST = 1;
	while(ADCON0.BIT.ADST == 1); // whait for conversion
	
	return (5.0/1023.0)*AD0.WORD; // return adc value
}

float temp() {
	ADCON0.BYTE = 0x07;	// Choose the AN7 as ADC input pin
	ADCON2.BYTE = 0x04;	// Use sample and hold and AD group P0
	
	ADCON0.BIT.ADST = 1;
	while(ADCON0.BIT.ADST == 1);

	return (((1.3/1023.0)*AD7.WORD) - 0.509)/0.00645;
}

void sleep(uint16 time) {
	// manage times from 1 us to 50 ms
	float count = time * (10000000.0/8.0);
	TA2 = (uint16)count;		// set the timer count value
	
	TABSR.BIT.TA2S = 1;			// start the timer
	while (TA2 != 0);			// it checks the same value 8 times since ftmr = fmcu/8 that should due
	TABSR.BIT.TA2S = 0;			// stop the timer
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -