用PICC写PIC16C57
1/ 57支持BANK限定词2/ 程序稍微复杂一些编译器就报BANK0无法分配的错误
3/ 程序用的局部变量过多时,程序虽然可编译通过,但是函数参数传递错误,需要尽量用小的数据类型,如 uchar变成uint,全局变量替代函数参数
4/ 单个程序过大要分成两个
5
/*
***************************************************
* FUNCTION description: I2C subfunction
function :send data to pt2323 and pt2258
editer: sunhongjian
date: 2004/2/19/am 11:30
**************************************************
*/
#include "pic165x.h"
#define uchar unsigned char
#define uintunsigned int
#define ul unsigned long
////////////////////////////////////////////////////
#define I2C_SCK RB6 //i2c
#define I2C_SDA RB7
#define SDA RB7
#define SCL RB6
#define PT2258_ADDRESS 0x80 //pt2258 address
#define PT2258_CLEAR 0xc0 //pt2258 clear command
///////////////////////////////////////////////////
#define STANDBY 0x44ba //customer key_value
#define MUTE 0xc43a
#define CEN_UP 0x45ba
#define SURR_UP 0x05fa
#define SUB_UP 0x847a
#define CEN_DOWN 0xc03e
#define SURR_DOWN0x50ae
#define SUB_DOWN 0x20de
#define FRONT_UP 0xd12e
#define VOL_UP 0x609e//
#define FRONT_DOWN 0x619e
#define VOL_DOWN 0xf50a
#define AUX1 0x21de
#define AUX2 0x718e
#define CH_5_1 0xb54a
#define CH_2_1_6 0x857a
#define RESET 0xa55a
#define UP 1
#define DOWN 0
#define VOL_INIT 0x3395
#define ADD 0x80
//////////////////////////////////
///////////////////////////////////////////////////
#define COM1 RA2
#define COM2 RA3
//////////////////////////////////////////////////
bank3 bit display_flag; //globe_val
bank3 uchar key_flag;//rf input flag
bank2 uchar c_flag;
bank2 ul key_value;//buffer of the rf key_value
bank2 uint k_f;
bank2 uchar display_buf; //two led data buffer
//bank2 uchar i2c_data;
//bank2 uchar i2c_comm;
bank1 uint c_vol;
bank2 uint vol_val = VOL_INIT;
bank2 uchar test;
bank3 uchar test1;
bank2 uint n;
bank2 uchar count;
bank3 uchar dis_data;
bank3 uchar t;
bank3 uint tt;
bank3 uchar ttt;
bank3 uchar t1;
const uchar a = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x27,0x7f,0x67};
//////////////////////////////////////////////////
void initial(void);
void two_led_display(uchar data);
void init_vol(void);
void delay_8ms(void);
uchar vol_cha(uchar k);
void select_ch(void);
void select_ch_1(void);
void delay_10_us(void);
void delay_n_ms(uchar n_milsecond);
void key_scan(void);
void at24c01_w(uchar at24c01_address);
void at24c01_r(uchar at24c01_address);
void vol_control(void);
voidmodify_data(uchar n ,uchar up_down);
////////////////////////////////////////////////////
void main(void)
{
initial();
init_vol();
/////////////////////////
for(;;)
{
if(test1) RB4 =0;
else RB4 =1;
if(!RA0 )
{
//two_led_display(vol_cha(6));
if(!RA0)
{
RA2 =0;
RA3 =0;//disable two_led display
key_scan();
select_ch();
select_ch_1();
vol_control();
}
}
else
{
two_led_display(vol_cha(6));
delay_8ms();
}
}
}
/*
//////////////////////////////////////////////////
function: initial fsr
time: 2004/2/19/pm 2:30
editer:sunhongjian
///////////////////////////////////////////////////
*/
void initial(void)
{
TRISA = 0x03; //RA0: input of rf
//RA2: two_led select
//RA3: two_led selcet
TRISB = 0x00; //RB7: data output of i2c
//RB6: sckof i2c
//RB4 RB3 RB2 RB1 RB0:output to control 5_number led
TRISC = 0x00; //RC6 RC5 RC4 RC3 RC2 RC1 RC0:data output of two_led
COM1 =0; //two_led put out
COM2 =0;
display_flag = 0;
PORTB = 0xff; //five_led put out
OPTION = 0x03;//
}
///////////////////////////////////////////////////
void delay_10_us(void)
{
#asm
nop;
nop;
nop;
nop;
nop;
#endasm
}
////////////////////////////////////////////////////
void delay_n_ms(uchar n_milsecond)
{
uchar i;
while(n_milsecond--)
{
i = 45;
while(i--);
}
}
//////////////////////////////////////////////
void delay_8ms(void)
{
TMR0 = 0;
while(TMR0 < 249);
}
///////////////////////////////////////////////////
bit i2c_start(void)
{
delay_10_us();
I2C_SDA = 1;
delay_10_us();
I2C_SCK = 1;
delay_10_us();
I2C_SDA = 0;
delay_10_us();
I2C_SCK = 0;
delay_10_us();
return 1;
}
////////////////////////////////////////////////////
void i2c_stop(void)
{
delay_10_us();
I2C_SDA = 0;
delay_10_us();
I2C_SCK = 1;
delay_10_us();
I2C_SDA = 1;
delay_10_us();
}
///////////////////////////////////////////////////
bit i2c_send_byte(uchar d)
{
uchar i = 8;
static bit bit_ack;
while(i--)
{
delay_10_us();
if(d & 0x80) I2C_SDA = 1;
else I2C_SDA = 0;
delay_10_us();
I2C_SCK =1;
delay_10_us();
I2C_SCK = 0;
d=d<<1;
}
delay_10_us();
I2C_SDA = 1;
delay_10_us();
I2C_SCK =1;
delay_10_us();
bit_ack =I2C_SDA;
I2C_SCK =0;
delay_10_us();
return bit_ack;
}
///////////////////////////////////////////
void at24c01_w1(uchar ic_address,uchar data)
{
i2c_start();
i2c_send_byte(ic_address);
i2c_send_byte(data);
i2c_stop();
delay_n_ms(10);////////////
}
/////////////////////////////////////////
void at24c01_w2(uint data)
{
i2c_start();
i2c_send_byte(0x88);
i2c_send_byte(data/256);
i2c_send_byte(data%256);
i2c_stop();
delay_n_ms(10);
}
/*
*****************************************************
*key_scan function
*description:read rf_key_value
*editer:sunhongjian
*timer:2004/2/19/pm/2:57
****************************************************
*/
void key_scan(void)
{
uchar k;
while(!RA0); //wait for 9ms low level
TMR0 = 0;
while(TMR0<150);
if(!RA0) c_flag =1;//continue key
if(!c_flag)
{
while(RA0); //wait for 4.5ms high level
for(k=0;k<32;k++)
{
while(!RA0);
TMR0 = 0;
while(TMR0 <54);
if(RA0)
{
key_value =key_value| 0x01;
while(RA0);
}
if(k!= 31) key_value<<=1;
}
n = key_value>>16;
if(n ==0x00ff )
{
k_f= key_value;
key_flag = 1;
return;
}
else key_value = 0;
}
/////////////////////continue key
else
{
c_flag =0;
++count;
}
}
//////////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////
void vol_control(void)
{
at24c01_w2(c_vol);
at24c01_w2(c_vol);
at24c01_w2(c_vol);
at24c01_w2(c_vol);
at24c01_w2(c_vol);
at24c01_w2(c_vol);
at24c01_w2(c_vol);
////////////////////
tt=c_vol;
tt=c_vol;
tt=c_vol;
tt=c_vol;
tt=c_vol;
tt=c_vol;
tt=c_vol;
//////////////
}
///////////////////////////////////
void init_vol(void)
{
c_vol =0x3395;
c_vol =0x4355;
c_vol =0x0315;
c_vol =0x2335;
c_vol =0x6375;
c_vol =0xa3b5;
c_vol =0xd3e5;
at24c01_w1(0x88,0xc0);
}
///////////////////////////////
uchar vol_cha(uchar k)
{
uchar i,j;
i = c_vol & 0x000f;
j = c_vol>>8;
j = j<<4;
j = i|j;
return j;
}
//////////////////////////////
void select_ch(void)
{
switch(k_f)
{
case CH_2_1_6:
k_f =0;//rf key value
at24c01_w1(0x94,0xff);//mute all
if(++t1%2)
{
at24c01_w1(0x94,0xfe);//6_output
}
else
{
at24c01_w1(0x94,0xf0);//fl
at24c01_w1(0x94,0xf2);//fr
at24c01_w1(0x94,0xf6);//sub
}
break;
case AUX1:
PORTB=0xfb;
k_f =0;//rf key value
at24c01_w1(0x94,0xff);//mute all
at24c01_w1(0x94,0xcb);
at24c01_w1(0x94,0xf0);//fl
at24c01_w1(0x94,0xf2);//fr
at24c01_w1(0x94,0xf6);//sub
break;
case AUX2:
PORTB=0xfb;
k_f =0;//rf key value
at24c01_w1(0x94,0xff);//mute all
at24c01_w1(0x94,0xca);
at24c01_w1(0x94,0xf0);//fl
at24c01_w1(0x94,0xf2);//fr
at24c01_w1(0x94,0xf6);//sub
break;
case CH_5_1:
PORTB=0xf7;
k_f =0;//rf key value
at24c01_w1(0x94,0xff);//mute all
at24c01_w1(0x94,0xc7);//6_input
at24c01_w1(0x94,0xfe);//6_output
break;
case STANDBY:
k_f =0;//rf key value
PORTB=0xfe;
case MUTE:
k_f =0;//rf key value
PORTB=0xfd;
if(t++%2)
at24c01_w1(0x94,0xff);
else
at24c01_w1(0x94,0xfe);//6_output
break;
case RESET:
c_vol =0x3395;
c_vol =0x4355;
c_vol =0x0315;
c_vol =0x2335;
c_vol =0x6375;
c_vol =0xa3b5;
c_vol =0xd3e5;
vol_val = VOL_INIT;
break;
default: break;
}
}
//////////////////////////////////////////////
void select_ch_1(void)
{
uchar i,j=0;
if((count>ADD || c_flag ==0)==0) return;
switch(k_f)
{
case CEN_UP: count = 0;
modify_data(2,UP);
return;
caseCEN_DOWN:
count = 0;
modify_data(2,DOWN);
return;
caseSURR_UP:
i=4;
j=UP;
caseSURR_DOWN:
i=4;
break;
caseSUB_UP:
count = 0;
modify_data(3,UP);
return;
caseSUB_DOWN:
count = 0;
modify_data(3,DOWN);
return;
caseFRONT_UP:
i=0;
j=UP;
caseFRONT_DOWN:
i=0;
break;
caseVOL_UP:
count = 0;
for(i=0;i<7;i++) modify_data(i,UP);
return;
caseVOL_DOWN:
count = 0;
for(i=0;i<7;i++) modify_data(i,DOWN);
return;
default :return;
}
count=0;
modify_data(i,j);
modify_data(i+1,j);
}
////////////////////////////////////
/*
**********************************************
*two_led display
*description control two_led display
*editor:sunhongjian
*timer:2004/2/19/pm/3
**********************************************
*/
void two_led_display(uchar data )
{
uchar data3;
data3 = (0xf0 & data);
data3>>=4;
data = (10 * data3) + (data & 0x0f);
data = 69-data;
if(display_flag ==0)
{
COM1 = 1;
COM2 = 0;
PORTC = a;
}
else
{
COM1 = 0;
COM2 = 1;
PORTC = a;
}
display_flag = !display_flag;
}
/////////////////////////////
voidmodify_data (uchar n ,uchar up_down)
{
uchar k;
ttt=n;
tt=c_vol;
k = vol_cha(n);
ttt=k;
if(k==0)
{
RB4 = 0;
if(!up_down)
++c_vol;
}
else if(k==105)
{
RB4 =0;
if(up_down)
--c_vol;
}
else
{
RB4 = !RB4;
if(up_down)
{
if((0x0f & k) == 0) c_vol = c_vol-0xf7;
else
--c_vol;
}
else
{
if((0x0f & k) == 9) c_vol = c_vol+0xf7;
else
++c_vol;
}
}
if(k==0 || k==105) test1 =1;//led
else test1 =0;
}
页:
[1]