蓝桥杯单片机(CT107D)需要准备的一些文件的写法(First Prepare for Lanqiao Cup MCU Competition)

实物图

CT107D.jpg

电路原理图

CT107D原理图.png
  • CT107D电路原理图

所有文件(all files)

  • 所有文件(源代码)
    common.h main.c timer.c hc138.c led.c drivers.c digital_tube.c key.c
  • TIPS:

x^1=~x; X^0=X;
Can't shift a single PIN, but can shift the entire I/O port;
(不可移位单个PIN,可移位整个I/O口);
Declare variable first, then operate;  //uchar i;  TR0=0;
(先声明变量,后操作);
  • common.h

common.h

#ifndef COMMON_H
#define COMMON_H

#include "stc15f2k60s2.h"
#include 

typedef unsigned char uchar;
typedef unsigned int uint;

#define seg_tab_none 19
#define bit_tab_none 8

extern uchar disp_val[8];
extern uchar trg,cont,key_val;
extern uchar ds1302_time[8];

extern void Timer0Init(void);
extern void Timer1Init(void);
extern void hc138Init(void);
extern void hc138_none(void);
extern void hc138_led(void);
extern void hc138_drivers(void);
extern void hc138_bit(void);
extern void hc138_seg(void);
extern void ledInit(void);
extern void led_on_N(uchar n);
extern void led_on_all(void);
extern void led_off(void);
extern void relayInit(void);
extern void buzzInit(void);
extern void dispscan(void);
extern void disp_val_none(void);
extern void keyscan(void);
extern uchar ds18b20_get(void);
extern void ds1302Init(void);
extern void ds1302_burst_read(uchar *val);

#endif
  • main.c

main.c

#include "common.h"

void SysInit(void)
{
  buzzInit();
  relayInit();
  ledInit();
  hc138Init();
  ds1302Init();
  Timer0Init();
  Timer1Init();
  ET0=1;    
  ET1=1;
  EA=1;
}

void main(void)
{
  SysInit();
  while(1)
  {
    ;
  }
}
  • timer.c

timer.c

#include "common.h"

void Timer0Init(void)       //[email protected]
{
  AUXR &= 0x7F;     //?????12T??
  TMOD &= 0xF0;     //???????
  TL0 = 0x66;       //??????
  TH0 = 0xFC;       //??????
  TF0 = 0;      //??TF0??
  TR0 = 1;      //???0????
}

void Timer1Init(void)       //[email protected]
{
  AUXR &= 0xBF;     //?????12T??
  TMOD &= 0x0F;     //???????
  TL1 = 0x00;       //??????
  TH1 = 0x4C;       //??????
  TF1 = 0;      //??TF1??
  TR1 = 1;      //???1????
}

void Timer0_Routine(void) interrupt 1
{
  //1ms
  static uint clock;
    static uchar select,temp;
    TR0=0;
    if(++clock==1000)
  {
    clock=0;
        if(++select==1)
        {
            ds1302_burst_read(ds1302_time);
            disp_val_none();
            disp_val[0]=ds1302_time[0]&0x0f;
            disp_val[1]=ds1302_time[0]>>4&0x0f;
            disp_val[2]=0x11;
            disp_val[3]=ds1302_time[1]&0x0f;
            disp_val[4]=ds1302_time[1]>>4&0x0f;
            disp_val[5]=0x11;
            disp_val[6]=ds1302_time[2]&0x0f;
            disp_val[7]=ds1302_time[2]>>4&0x0f;
        }
        else
        {
            select=0;
            temp=ds18b20_get();
            disp_val_none();
            disp_val[0]=0x0c;
            disp_val[1]=0x12;
            disp_val[2]=temp%10;
            disp_val[3]=temp/10;
        }
    }
    dispscan();
  TR0=1;
}

void Timer1_Routine(void) interrupt 3
{
  //50ms
  TR1=0;
  keyscan();
  if(trg)
  {
    switch(key_val)
    {
      case 1:led_on_all();break;
      case 2:break;
      case 3:break;
      case 4:break;
      case 5:break;
      case 6:led_on_N(0xaa);break;
      case 7:break;
      case 8:break;
      case 9:break;
      case 10:break;
      case 11:led_on_N(0x55);break;
      case 12:break;
      case 13:break;
      case 14:break;
      case 15:break;
      case 16:led_off();break;
      default:break;
    }
  }
  TR1=1;
}
  • hc138.c

hc138.c

#include "common.h"

#define HC138 P2

void hc138Init(void)
{
  HC138&=0x1f;
}

void hc138_none(void)
{
  HC138&=0x1f;
}

void hc138_led(void)
{
  HC138&=0x1f;
  HC138|=0x80;
}

void hc138_drivers(void)
{
  HC138&=0x1f;
  HC138|=0xa0;
}

void hc138_bit(void)
{
  HC138&=0x1f;
  HC138|=0xc0;
}

void hc138_seg(void)
{
  HC138&=0x1f;
  HC138|=0xe0;
}
  • led.c

led.c

#include "common.h"

#define LED P0

void ledInit(void)
{
  hc138_led();
  LED=0xff;
  hc138_none();
}

void led_on_N(uchar n)
{
  hc138_led();
  LED=n;
  hc138_none();
}

void led_on_all(void)
{
  hc138_led();
  LED=0x00;
  hc138_none();
}

void led_off(void)
{
  hc138_led();
  LED=0xff;
  hc138_none();
}
  • drivers.c

drivers.c

#include "common.h"

sbit RELAY_PIN=P0^4;
sbit BUZZ_PIN=P0^6;

void relayInit(void)
{
  hc138_drivers();
  RELAY_PIN=0;
  hc138_none();
}

void buzzInit(void)
{
  hc138_drivers();
  BUZZ_PIN=0;
  hc138_none();
}
  • digital_tube.c

digital_tube.c

#include "common.h"

#define BIT P0
#define SEG P0

code uchar seg_tab[20]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
                        0x88,0x83,0xc6,0xa1,0x86,0x8e,
                        0x7f,0xbf,0x9c,0xff};  //0~9,A~F,.,-,o,blank
code uchar bit_tab[9]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x00};
uchar disp_val[8]={seg_tab_none,seg_tab_none,seg_tab_none,seg_tab_none,
                   seg_tab_none,seg_tab_none,seg_tab_none,seg_tab_none};

void disp_val_none(void)
{
    uchar i;
    for(i=0;i<8;i++)
    {
        disp_val[i]=seg_tab_none;
    }
}
                                     
void dispscan(void)
{
  static uchar pointer=0;
  SEG=seg_tab[seg_tab_none];
  hc138_seg();
  SEG=seg_tab[disp_val[pointer]];
  hc138_none();
  BIT=bit_tab[bit_tab_none];
  hc138_bit();
  BIT=bit_tab[pointer];
  if(++pointer==8)
  {
    pointer=0;
  }
  hc138_none();
}
  • key.c

key.c

#include "common.h"

#define KEY P3

uchar trg,cont,trg_row,cont_row,trg_column,cont_column,key_val;

void keyscan(void)
{
  uchar readdata=0x00,P42_val=0,P44_val=0;
  KEY=0x0f&0x3f;
  P42=0;
  P44=0;
  P42_val=P42;
  P44_val=P44;
  P42_val=P44_val<<6&0x40;
  P44_val=P44_val<<7&0x80;
  readdata=KEY&0x3f|P42_val|P44_val;
  readdata^=0x0f;
  trg_row=readdata&(readdata^cont_row);
  cont_row=readdata;
  KEY=0xf0&0x3f;
  P42=1;
  P44=1;
  P42_val=P42;
  P44_val=P44;
  P42_val=P42_val<<6&0x40;
  P44_val=P44_val<<7&0x80;
  readdata=KEY&0x3f|P42_val|P44_val;
  readdata^=0xf0;
  trg_column=readdata&(readdata^cont_column);
  cont_column=readdata;
  trg=trg_row|trg_column;
  cont=cont_row|cont_column;
  switch(trg)
  {
  case 0x81:key_val=1;break;
  case 0x82:key_val=2;break;
  case 0x84:key_val=3;break;
  case 0x88:key_val=4;break;
  case 0x41:key_val=5;break;
  case 0x42:key_val=6;break;
  case 0x44:key_val=7;break;
  case 0x48:key_val=8;break;
  case 0x21:key_val=9;break;
  case 0x22:key_val=10;break;
  case 0x24:key_val=11;break;
  case 0x28:key_val=12;break;
  case 0x11:key_val=13;break;
  case 0x12:key_val=14;break;
  case 0x13:key_val=15;break;
  case 0x18:key_val=16;break;
  default:break;
  }
  KEY=0xff;
}
  • ds18b20.c

ds18b20.c

#include "common.h"

sbit DQ=P1^4;

void Delay50us()        //@11.0592MHz
{
    unsigned char i, j;

    _nop_();
    i = 1;
    j = 134;
    do
    {
        while (--j);
    } while (--i);
}

void Delay500us()       //@11.0592MHz
{
    unsigned char i, j;

    _nop_();
    _nop_();
    i = 6;
    j = 93;
    do
    {
        while (--j);
    } while (--i);
}

void ds18b20Init(void)
{
    DQ=0;
    Delay500us();
    DQ=1;
    Delay50us();
    while(~DQ);
}

void write_byte(uchar val)
{
    uchar i=0x00;
    for(i=0x01;i;i<<=1)
    {
        DQ=0;
        DQ=val&i;
        Delay50us();
        Delay50us();
        DQ=1;
    }
}

uchar read_byte()
{
    uchar i=0x00,val=0;
    for(i=0x01;i;i<<=1)
    {
        DQ=0;
        DQ=1;
        if(DQ)val=val|i;
        Delay50us();
        Delay50us();
    }
    return val;
}

uchar ds18b20_get(void)
{
    static uchar temp[2]={0x00,0x00},val;
    ds18b20Init();
    write_byte(0xcc);
    write_byte(0x44);
    ds18b20Init();
    write_byte(0xcc);
    write_byte(0xbe);
    temp[0]=read_byte();
    temp[1]=read_byte();
    val=temp[0]>>4&0x0f|temp[1]<<4&0xf0;
    return val;
}
  • ds1302.c (burst_mode)

ds1302.c

#include "common.h"

sbit CE=P1^3;
sbit SCLK=P1^7;
sbit I_O=P2^3;

uchar ds1302_time[8];
code uchar Inittime[]={0x05,0x20,0x19,0x23,0x02,0x06,0x19};

void ds1302_write_byte(uchar dat)
{
    uchar i=0x00;
    for(i=0x01;i;i<<=1)
    {
        SCLK=0;
        if(dat&i)
        {
            I_O=1;
        }
        else
        {
            I_O=0;
        }
        SCLK=1;
    }
    I_O=1;
}

uchar ds1302_read_byte(void)
{
    uchar i=0x00,val=0x00;
    for(i=0x01;i;i<<=1)
    {
        SCLK=0;
        if(I_O)
        {
            val|=i;
        }
        SCLK=1;
    }
    return val;
}

void ds1302_single_write(uchar reg,uchar dat)
{
    SCLK=0;
    I_O=0;
    CE=1;
    ds1302_write_byte(reg<<1|0x80);
    ds1302_write_byte(dat);
    CE=0;
}

uchar ds1302_single_read(uchar reg)
{
    uchar val;
    SCLK=0;
    I_O=0;
    CE=1;
    ds1302_write_byte(reg<<1|0x81);
    val=ds1302_read_byte();
    CE=0;
    return val;
}

void ds1302_burst_write(uchar *dat)
{
    uchar i=0;
    SCLK=0;
    I_O=0;
    CE=1;
    ds1302_write_byte(0xbe);
    for(i=0;i<8;i++)
    {
        ds1302_write_byte(dat[i]);
    }
    CE=0;
}

void ds1302_burst_read(uchar *val)
{
    uchar i=0;
    SCLK=0;
    I_O=0;
    CE=1;
    ds1302_write_byte(0xbf);
    for(i=0;i<8;i++)
    {
        val[i]=ds1302_read_byte();
    }
    CE=0;
}

void ds1302Init(void)
{
    CE=0;
    SCLK=0;
    if(ds1302_single_read(0)&0x80)
    {
        ds1302_single_write(7,0x00);
        ds1302_burst_write(Inittime);
    }
}


  • [x] common.h
  • [x] main.c
  • [x] timer.c
  • [x] hc138[1].c --->数据手册
  • [x] led.c
  • [x] drivers[2].c
  • [x] digital_tube.c
  • [x] key.c
  • [x] ds18b20[3]
  • [ ] ds1302[4]
  • [ ] IIC
  • [ ] 客观题

  1. 3位译码器 ↩

  2. 步进电机,直流电机,继电器,蜂鸣器,功放 ↩

  3. 温度传感器 ↩

  4. 时钟芯片 ↩

你可能感兴趣的:(蓝桥杯单片机(CT107D)需要准备的一些文件的写法(First Prepare for Lanqiao Cup MCU Competition))