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

📄 97_bike_for_auto_control_speed.c

📁 Interfacing H1632 with PIC 18F452
💻 C
字号:
// crystal 4MHz with PLL enable
#include "p18f242.h"
#include "type_define.h"

#pragma config OSC = HSPLL  //XT
#pragma config PWRT = ON
#pragma config WDT = OFF
#pragma config LVP = OFF

#define CS			PORTCbits.RC2	//output
#define DATA		PORTCbits.RC3	//output
#define WR			PORTCbits.RC4	//output

#define TRUE			1
#define FALSE			0

#define bit_flash       flag1.bit0
#define bit_set_time    flag1.bit1
#define bit_set_spd     flag1.bit2
#define bit_set_watt    flag1.bit3
#define bit_set_dist    flag1.bit4
#define bit_set_kcal    flag1.bit7

void isr_high_code(void);
void isr_high(void);
void mcu_io_init(void);
void init_val(void);
void led_cmd(void);
void trans_led(void);
void display_time(void);
void display_watt(void);
void display_kcal(void);
void display_spd(void);
void display_dist(void);
void isr_low_code(void);
void isr_low(void);

typedef struct
{
	unsigned bit0:1;
    unsigned bit1:1;
    unsigned bit2:1;
    unsigned bit3:1;
    unsigned bit4:1;
    unsigned bit5:1;
    unsigned bit6:1;
    unsigned bit7:1;
}uns8_bits;

typedef union
{
	uns8_bits t_temp;
	uns8 uns8_t_temp;
}union_uns8_bits;//t_temp;

union
{
	uns8_bits led_ram_map[46];
	uns8 uns8_led_ram_map[46];
}union_led_ram_map;

const rom uns8 number_table[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67,0x00};
                                // 0    1    2    3    4    5    6    7    8    9  SPACE
uns8_bits flag1;
uns8 number,set_min,min,sec,set_watt,watt,set_spd,spd;
union_uns8_bits union_t_temp,union_t_temp1,union_t_temp2,union_t_temp3;
uns16 send_word,kcal,set_dist,dist;

#pragma code hi_vector=0x0008
void isr_high_code(void)
{
	_asm
		goto	isr_high
	_endasm
}
#pragma code	/* return to the default code section */

#pragma code lo_vector=0x0018
void isr_low_code(void)
{
	_asm
		goto	isr_low
	_endasm
}
#pragma code	/* return to the default code section */

//#pragma interrupt isr_high// nosave=section(".tmpdata")//save=PROD//,section(".tmpdata"),TABLAT,TBLPTR,PCLATH,PCLATU
#pragma interrupt isr_high nosave=section(".tmpdata")
void isr_high(void)
{
	if(TMR0IF && TMR0IE)	//Timer0 interrupt,250ms interrupt one time
	{
		TMR0IF=0;
	}
	if(ADIE && ADIF)
	{
		ADIF=0;
	}
	if(TMR1IE && TMR1IF)//10ms interrupt one time
	{
		TMR1IF=0;
	}
	if(TMR2IE && TMR2IF)//100us interrupt one time
	{
		TMR2IF=0;
	}
	if(INT0IE && INT0IF)
	{
		INT0IF=0;
	}
	if(INT1IE && INT1IF)
	{
		INT1IF=0;
	}
}	
#pragma code

#pragma interruptlow isr_low nosave=section(".tmpdata")//save=PROD//,section(".tmpdata"),TABLAT,TBLPTR,PCLATH,PCLATU
void isr_low(void)
{
}
#pragma code

void main(void)
{
  uns8 uns8_temp;
	mcu_io_init();
	init_val();
	while(1)
	{
    watt=12;
    display_watt();
    spd=150;
    display_spd();
    dist=987;
    display_dist();
    kcal=456;
    display_kcal();
    min=30;
    sec=0;
    display_time();
		trans_led();
	}
}

void mcu_io_init(void)
{
//I/O setting
	// PORTA setting
	PORTA=0x00;
	LATA=0x00;
	ADCON1=0b10000100;
	TRISA=0xff;
	// PORTB setting
	PORTB=0x00;
	LATB=0x00;
	TRISB=0b11011111;
	// PORTC setting
	PORTC=0x00;
	LATC=0x00;
	TRISC=0b10100000;
//I/O setting

	WR=1;
	DATA=1;
	CS=1;
	send_word=0b1000000000100000;
	led_cmd();	//Turn on system oscillator(100-0000-0001-X)
	send_word=0b1000000001100000;
	led_cmd();	//Turn ON LED display(100-0000-0011-X)
	send_word=0b1000010010000000;
	led_cmd();	//N-MOS open drain output and 16 common option(100-0010-01XX-X)
	send_word=0b1001010111100000;
	led_cmd();	//PWM 16/16 duty(100-101X-1111-X)
}

void init_val(void)
{
	uns8 i;
	for(i=0;i<46;i++)
	{
		union_led_ram_map.uns8_led_ram_map[i]=0x00;
	}
  bit_flash=FALSE;
  bit_set_time=FALSE;
  bit_set_spd=FALSE;
  bit_set_watt=FALSE;
  bit_set_dist=FALSE;
  bit_set_kcal=FALSE;
	set_min=0;
	min=0;
	sec=0;
	set_watt=0;
	watt=0;
	kcal=0;
	set_spd=50;
	spd=0;
	set_dist=0;
	dist=0;
	union_led_ram_map.led_ram_map[14].bit7=1;//display dist dot
}
void led_cmd(void)
{
	uns8 i;
	CS=0;
	for(i=1;i<=12;i++)
	{
		WR=0;
		if(send_word>=0x8000)
			DATA=1;
		else
			DATA=0;
		send_word=send_word<<1;
    WR=1;
	}
	CS=1;
}
void trans_led(void)
{
	uns8 i,j;
	uns16 w_cmd_addr;	//write command:101,addr=00000000
	w_cmd_addr=0xa000;
	CS=0;
	for(i=0;i<10;i++)
	{
		WR=0;
		if(w_cmd_addr>=0x8000)
			DATA=1;
		else
			DATA=0;
		w_cmd_addr=w_cmd_addr<<1;
		WR=1;
	}
	for(i=0;i<46;i++)
	{
		union_t_temp.uns8_t_temp=union_led_ram_map.uns8_led_ram_map[i];
		for(j=0;j<8;j++)
		{
			WR=0;
			DATA=union_t_temp.t_temp.bit0;
			union_t_temp.uns8_t_temp=union_t_temp.uns8_t_temp>>1;
      WR=1;
		}
	}
	CS=1;
}
void display_time(void)
{
  uns8 uns8_temp_min,uns8_temp_sec;
  if(bit_flash==FALSE && bit_set_time==TRUE)
  {
    union_t_temp2.uns8_t_temp=0x00;  //SPACE
    union_t_temp3.uns8_t_temp=0x00;
  }
  else
  {
    if(bit_set_time==TRUE)
    {
      uns8_temp_min=set_min;
      uns8_temp_sec=0;
    }
    else
    {
      uns8_temp_min=min;
      uns8_temp_sec=sec;
    }
    number=uns8_temp_sec%10;
    union_t_temp.uns8_t_temp=number_table[number];
    number=uns8_temp_sec/10;
	  union_t_temp1.uns8_t_temp=number_table[number];
    number=uns8_temp_min%10;
    union_t_temp2.uns8_t_temp=number_table[number];
    number=uns8_temp_min/10;
	  union_t_temp3.uns8_t_temp=number_table[number];
  }
	union_led_ram_map.led_ram_map[16].bit2=union_t_temp.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[18].bit2=union_t_temp.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[20].bit2=union_t_temp.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[22].bit2=union_t_temp.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[24].bit2=union_t_temp.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[26].bit2=union_t_temp.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[28].bit2=union_t_temp.t_temp.bit6;	//G
	
	union_led_ram_map.led_ram_map[16].bit3=union_t_temp1.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[18].bit3=union_t_temp1.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[20].bit3=union_t_temp1.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[22].bit3=union_t_temp1.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[24].bit3=union_t_temp1.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[26].bit3=union_t_temp1.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[28].bit3=union_t_temp1.t_temp.bit6;	//G
	
	union_led_ram_map.led_ram_map[16].bit4=union_t_temp2.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[18].bit4=union_t_temp2.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[20].bit4=union_t_temp2.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[22].bit4=union_t_temp2.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[24].bit4=union_t_temp2.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[26].bit4=union_t_temp2.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[28].bit4=union_t_temp2.t_temp.bit6;	//G
	
	union_led_ram_map.led_ram_map[16].bit5=union_t_temp3.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[18].bit5=union_t_temp3.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[20].bit5=union_t_temp3.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[22].bit5=union_t_temp3.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[24].bit5=union_t_temp3.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[26].bit5=union_t_temp3.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[28].bit5=union_t_temp3.t_temp.bit6;	//G
}
void display_watt(void)
{
  uns8 uns8_temp_watt;
  if(bit_flash==FALSE && bit_set_watt==TRUE)
  {
    union_t_temp.uns8_t_temp=0x00;  //SPACE
    union_t_temp1.uns8_t_temp=0x00;
  }
  else
  {
    if(bit_set_watt==TRUE)
      uns8_temp_watt=set_watt;
    else
      uns8_temp_watt=watt;
    number=uns8_temp_watt%10;
    union_t_temp.uns8_t_temp=number_table[number];
    number=uns8_temp_watt/10;
	  union_t_temp1.uns8_t_temp=number_table[number];
  }
	union_led_ram_map.led_ram_map[16].bit0=union_t_temp.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[18].bit0=union_t_temp.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[20].bit0=union_t_temp.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[22].bit0=union_t_temp.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[24].bit0=union_t_temp.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[26].bit0=union_t_temp.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[28].bit0=union_t_temp.t_temp.bit6;	//G
	
	union_led_ram_map.led_ram_map[16].bit1=union_t_temp1.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[18].bit1=union_t_temp1.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[20].bit1=union_t_temp1.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[22].bit1=union_t_temp1.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[24].bit1=union_t_temp1.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[26].bit1=union_t_temp1.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[28].bit1=union_t_temp1.t_temp.bit6;	//G
}
void display_kcal(void)
{
  uns8 uns8_temp_kcal;
  if(bit_flash==FALSE && bit_set_kcal==TRUE)
  {
    union_t_temp.uns8_t_temp=0x00;  //SPACE
    union_t_temp1.uns8_t_temp=0x00;
    union_t_temp2.uns8_t_temp=0x00;
  }
  else
  {
    number=kcal%10;
    union_t_temp.uns8_t_temp=number_table[number];
    uns8_temp_kcal=kcal/10;
    number=uns8_temp_kcal%10;
    union_t_temp1.uns8_t_temp=number_table[number];
    number=uns8_temp_kcal/10;
    union_t_temp2.uns8_t_temp=number_table[number];
  }
	union_led_ram_map.led_ram_map[0].bit3=union_t_temp.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[2].bit3=union_t_temp.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[4].bit3=union_t_temp.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[6].bit3=union_t_temp.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[8].bit3=union_t_temp.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[10].bit3=union_t_temp.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[12].bit3=union_t_temp.t_temp.bit6;	//G
	
	union_led_ram_map.led_ram_map[0].bit4=union_t_temp1.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[2].bit4=union_t_temp1.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[4].bit4=union_t_temp1.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[6].bit4=union_t_temp1.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[8].bit4=union_t_temp1.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[10].bit4=union_t_temp1.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[12].bit4=union_t_temp1.t_temp.bit6;	//G
	
	union_led_ram_map.led_ram_map[0].bit5=union_t_temp2.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[2].bit5=union_t_temp2.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[4].bit5=union_t_temp2.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[6].bit5=union_t_temp2.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[8].bit5=union_t_temp2.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[10].bit5=union_t_temp2.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[12].bit5=union_t_temp2.t_temp.bit6;	//G
}
void display_spd(void)
{
  uns8 uns8_temp_spd;
  if(bit_flash==FALSE && bit_set_spd==TRUE)
  {
    union_t_temp.uns8_t_temp=0x00;  //SPACE
    union_t_temp1.uns8_t_temp=0x00;
    union_t_temp2.uns8_t_temp=0x00;
  }
  else
  {
    if(bit_set_spd==TRUE)
      uns8_temp_spd=set_spd;
    else
      uns8_temp_spd=spd;
    number=uns8_temp_spd%10;
    union_t_temp.uns8_t_temp=number_table[number];
  	uns8_temp_spd=uns8_temp_spd/10;
    number=uns8_temp_spd%10;
	  union_t_temp1.uns8_t_temp=number_table[number];
    number=uns8_temp_spd/10;
	  union_t_temp2.uns8_t_temp=number_table[number];
  }
	union_led_ram_map.led_ram_map[0].bit0=union_t_temp.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[2].bit0=union_t_temp.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[4].bit0=union_t_temp.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[6].bit0=union_t_temp.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[8].bit0=union_t_temp.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[10].bit0=union_t_temp.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[12].bit0=union_t_temp.t_temp.bit6;	//G
	
	union_led_ram_map.led_ram_map[0].bit1=union_t_temp1.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[2].bit1=union_t_temp1.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[4].bit1=union_t_temp1.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[6].bit1=union_t_temp1.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[8].bit1=union_t_temp1.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[10].bit1=union_t_temp1.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[12].bit1=union_t_temp1.t_temp.bit6;	//G
	
	union_led_ram_map.led_ram_map[0].bit2=union_t_temp2.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[2].bit2=union_t_temp2.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[4].bit2=union_t_temp2.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[6].bit2=union_t_temp2.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[8].bit2=union_t_temp2.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[10].bit2=union_t_temp2.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[12].bit2=union_t_temp2.t_temp.bit6;	//G
}
void display_dist(void) //display 00.0
{
  uns16 uns16_temp_dist;
  if(bit_flash==FALSE && bit_set_dist==TRUE)
  {
    union_t_temp.uns8_t_temp=number_table[0];  //display 0
    union_t_temp1.uns8_t_temp=0x00; //SPACE
    union_t_temp2.uns8_t_temp=0x00;
  }
  else
  {
    if(bit_set_dist==TRUE)
      uns16_temp_dist=set_dist;
    else
      uns16_temp_dist=dist;
    number=uns16_temp_dist%10;
    union_t_temp.uns8_t_temp=number_table[number];
  	uns16_temp_dist=uns16_temp_dist/10;
    number=uns16_temp_dist%10;
	  union_t_temp1.uns8_t_temp=number_table[number];
    number=uns16_temp_dist/10;
	  union_t_temp2.uns8_t_temp=number_table[number];
  }
	union_led_ram_map.led_ram_map[0].bit6=union_t_temp.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[2].bit6=union_t_temp.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[4].bit6=union_t_temp.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[6].bit6=union_t_temp.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[8].bit6=union_t_temp.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[10].bit6=union_t_temp.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[12].bit6=union_t_temp.t_temp.bit6;	//G
	
	union_led_ram_map.led_ram_map[0].bit7=union_t_temp1.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[2].bit7=union_t_temp1.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[4].bit7=union_t_temp1.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[6].bit7=union_t_temp1.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[8].bit7=union_t_temp1.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[10].bit7=union_t_temp1.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[12].bit7=union_t_temp1.t_temp.bit6;	//G
	
	union_led_ram_map.led_ram_map[1].bit0=union_t_temp2.t_temp.bit0;	//A
	union_led_ram_map.led_ram_map[3].bit0=union_t_temp2.t_temp.bit1;	//B
	union_led_ram_map.led_ram_map[5].bit0=union_t_temp2.t_temp.bit2;	//C
	union_led_ram_map.led_ram_map[7].bit0=union_t_temp2.t_temp.bit3;	//D
	union_led_ram_map.led_ram_map[9].bit0=union_t_temp2.t_temp.bit4;	//E
	union_led_ram_map.led_ram_map[11].bit0=union_t_temp2.t_temp.bit5;	//F
	union_led_ram_map.led_ram_map[13].bit0=union_t_temp2.t_temp.bit6;	//G
}

⌨️ 快捷键说明

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