电子技术
想用计时器做个数码管0-99秒表,但是数码管连亮都不亮
懒惰的0族元素 2016-6-16 11:08:13
#include<reg52.h>
#define uint unsigned int
#define	uchar unsigned char
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uint tent,sec;
bit write=0;
sbit sda=P1^2;
sbit scl=P1^7;
sbit dula=P2^6;
sbit wela=P2^7;
void delay()
{;;}
void delay1ms(uint z)
{
uint x,y;
for(x=0;x<z;x++)
for(y=0;y<110;y++);
}
void start()
{
 sda=1;
 delay();
 scl=1;
 delay();
 sda=0;
 delay();
}
void stop()
{
 sda=0;
 delay();
 scl=1;
 delay();
 sda=1;
 delay();
}
void response()
{
uint i;
scl=1;
while((sda==1)&&(i<250))
i++;
scl=0;
delay();
}
void init()
{
scl=1;
delay();
sda=1;
delay();
}
void write_byte(uchar date)
{
uchar temp,i;
temp=date;
for(i=0;i<8;i++)
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
uchar read_byte()
{
uchar k,i;
for(i=0;i<8;i++)
{
scl=0;
delay();
k=(k<<1)|sda;
dealy();
scl=1;
delay();
}
return k;
}
void write_24c02(uchar adress,uchar date)
{
start();
write_byte(0xa0);
response();
write_byte(adress);
response();
write_byte(date);
response();
stop();
}
uchar read_add(uchar adress)
{
  uchar date;
  start();
  write_byte(0xa0);
  response();
  write_byte(adress);
  response();
  start();
  write_byte(0xa1);
  response();
  date=read_byte();
  stop();
  return date;
}
void display(uchar shi,uchar ge)
{
 dula=1;
 P0=table[shi];
 dula=0;
 wela=1;
 P0=0x00;
 wela=0;
 delay1ms(5);
 dula=1;
 P0=table[ge];
 dula=0;
 wela=1;
 P0=0x00;
 wela=0;
 delay1ms(5);
}
void main()
{
 init();
 sec=read_add(2);							
 if(sec>100)
 sec=0;
 TMOD=0x01;
 TH0=(65535-50000)/256;
 TL0=(65535-50000)%256;
 EA=1;
 ET0=1;
 TR0=1;
 while(1)
 {
 display(sec/10,sec%10);
 if(write==1)
 {
  write=0;
  write_24c02(2,sec);
 }
 }
}
void qwe()interrupt 1
{
 TH0=(65535-50000)/256;
 TL0=(65535-50000)%256;
 tent++;
 if(tent=20)
 {
 tent=0;
 sec++;
 write=1;
 if(sec==100)
 sec=0;
 }
}

[修改于 3 年前 - 2016-06-16 12:22:19]

2016-6-16 12:23:31
1楼

已经帮楼主编辑。包含代码请使用backtick(数字1左边的键),具体参考markdown规范。

2楼

虽然不知道硬件电路,但是十分可能是某个地方的delay()延时不够久。

2016-6-20 21:16:40
3楼

给你个参考 display.c

#include "stm8s.h"
#include "display.h"

#define A(a) a ? (GPIOA->ODR |= GPIO_PIN_1):(GPIOA->ODR &= (~GPIO_PIN_1))
#define B(a) a ? (GPIOF->ODR |= GPIO_PIN_4):(GPIOF->ODR &= (~GPIO_PIN_4))
#define C(a) a ? (GPIOC->ODR |= GPIO_PIN_5):(GPIOC->ODR &= (~GPIO_PIN_5))
#define D(a) a ? (GPIOC->ODR |= GPIO_PIN_7):(GPIOC->ODR &= (~GPIO_PIN_7))
#define E(a) a ? (GPIOD->ODR |= GPIO_PIN_0):(GPIOD->ODR &= (~GPIO_PIN_0))
#define F(a) a ? (GPIOA->ODR |= GPIO_PIN_2):(GPIOA->ODR &= (~GPIO_PIN_2))
#define G(a) a ? (GPIOC->ODR |= GPIO_PIN_4):(GPIOC->ODR &= (~GPIO_PIN_4))
#define DP(a) a ? (GPIOC->ODR |= GPIO_PIN_6):(GPIOC->ODR &= (~GPIO_PIN_6))
//                                0    1    2    3    4    5    6    7    8    9    P   -
const uint8_t display_data[] = {0xFC,0X60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFF,0xF6,0xCE,0x02};
//void Display_b(uint8_t data);
//void Display_3(uint16_t data,uint8_t dp,uint8_t dig);
//显示刷新函数
/*
uint8_t dp  == 小数点高有效 3'b000 ~ 3'b111
*/

void Display(uint8_t dp)
{
  extern uint8_t dig_1,dig_2,dig_3,display_cnt;
  
      
     switch(display_cnt)
      {

        case 0: GPIOD->ODR &= (~GPIO_PIN_7);//DIG3_LOW
                A(dig_1 & 0X80);
                B(dig_1 & 0X40);
                C(dig_1 & 0X20);
                D(dig_1 & 0X10);
                E(dig_1 & 0x08);
                F(dig_1 & 0X04);
                G(dig_1 & 0X02);
                DP(dp & 0x04);
                GPIOD->ODR |= GPIO_PIN_3;//DIG1_HIGH 
                break;
        
        case 1: GPIOD->ODR &= (~GPIO_PIN_3);//DIG1_LOW            
                A(dig_2 & 0X80);
                B(dig_2 & 0X40);
                C(dig_2 & 0X20);
                D(dig_2 & 0X10);
                E(dig_2 & 0x08);
                F(dig_2 & 0X04);
                G(dig_2 & 0X02);
                DP(dp & 0x02);
                GPIOD->ODR |= GPIO_PIN_2;//DIG2_HIGH
                break;
        case 2: GPIOD->ODR &= (~GPIO_PIN_2);//DIG2_LOW
                A(dig_3 & 0X80);
                B(dig_3 & 0X40);
                C(dig_3 & 0X20);
                D(dig_3 & 0X10);
                E(dig_3 & 0x08);
                F(dig_3 & 0X04);
                G(dig_3 & 0X02);
                DP(dp & 0x01);
                GPIOD->ODR |= GPIO_PIN_7;//DIG3_HIGH
                break;
      
  }
  display_cnt++;
  if(display_cnt == 3)
    display_cnt = 0;
  
  
}

void Display_Init(void)
{
  //PA 1 2
  GPIO_Init(GPIOA,(GPIO_Pin_TypeDef)(GPIO_PIN_1 | GPIO_PIN_2), GPIO_MODE_OUT_PP_LOW_FAST);
  //PC 4 5 6 7
  GPIO_Init(GPIOC,(GPIO_Pin_TypeDef)(GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7), GPIO_MODE_OUT_PP_LOW_FAST);
  //PD 0 2 3 7
  GPIO_Init(GPIOD,(GPIO_Pin_TypeDef)(GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_7), GPIO_MODE_OUT_PP_LOW_FAST);
  //PF 4
  GPIO_Init(GPIOF,GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST);
  

}

/*
数码管显示数据刷新
uint16_t data == 显示数据 0 ~ 999  
*/
void Display_update(uint16_t data)
{
  extern uint8_t dig_1,dig_2,dig_3; 
  dig_1 = display_data[((uint8_t)(data/100))] ;
  dig_2 = display_data[((uint8_t)((data%100)/10))];
  dig_3 = display_data[((uint8_t)(data%10))];
  
}

display.h

#include "stm8s.h"
#ifndef DISPLAY_H_
#define DISPLAY_H_

void Display_Init(void);//数码管初始化
void Display(uint8_t dp);//数码管刷新
void Display_update(uint16_t data);//数码管显示数据更新



#endif

我是stm8io冲突没法使用整组io只能这么搞了 每次调用显示函数移位一次每次显示小号69 -71时钟周期

2016-7-6 01:23:58
4楼
这个东西光看软件是不行的,要和硬件(电路)对应起来找问题
2016-7-8 23:18:34
5楼
自己编的,看看就行

ORG	00H
AJMP	MAIN
ORG        0BH
AJMP	T0INT
ORG	30H	   
MAIN:LCALL INIT	   ;初始化
START:LCALL DISP   ;开始
      JB P1.4,PAUSE
	  SETB	TR0
	  AJMP START
PAUSE: LCALL DISP      //暂停
       JB P1.5,CLEAR
      CLR TR0
	   AJMP PAUSE
CLEAR: LCALL DISP      //清零
       JB P1.6,START
       CLR TR0
	   MOV R4,#0
	   MOV R0,#0
	   AJMP CLEAR
DISP:			  //显示
MOV	A,R4
MOV	B,#10
DIV	AB
MOV DPTR,#TAB		  //不带小数点显示
MOVC	A,@A+DPTR
MOV	P0,A
CLR	P1.0
CLR	P1.2
CLR	P1.3
ACALL	D1MS
SETB	P1.0
SETB	P1.2
SETB	P1.3
MOV	A,B
MOVC	A,@A+DPTR
MOV	P0,A
CLR	P1.1
CLR	P1.2
CLR	P1.3
ACALL	D1MS
SETB	P1.1
SETB	P1.2
SETB	P1.3
ACALL	D1MS
MOV	A,R0
MOV	B,#10
DIV	AB
MOV DPTR,#TAB
MOVC	A,@A+DPTR
MOV	P0,A
CLR	P1.2
CLR	P1.0
CLR	P1.1
ACALL	D1MS
SETB	P1.2
SETB	P1.0
SETB	P1.1
MOV	A,B
MOV DPTR,#TABEL			 ;带小数点显示
MOVC	A,@A+DPTR
MOV	P0,A
CLR	P1.3
CLR	P1.0
CLR	P1.1
ACALL	D1MS
SETB	P1.3
SETB	P1.0
SETB	P1.1
RET
INIT:MOV 	SP,#60H
MOV	R4,#0
MOV	DPTR,#TAB
MOV	TMOD,#01H   
MOV	TH0,#0D8H   ; 10MS
MOV	TL0,#0F0H	
SETB	ET0
SETB	EA

RET		
T0INT:	
MOV	TH0,#0D8H
MOV	TL0,#0F0H
INC	R4
CJNE	R4,#100,RTI	;10MS X 100 = 1S 
MOV	R4,#0
INC	R0
CJNE	R0,#100,RTI
MOV	R0,#0	
RTI:	
RETI

D1MS:
MOV R7,#01H
DL1:
    MOV R6,#8EH
DL0:
    MOV R5,#02H
    DJNZ R5,$
    DJNZ R6,DL0
    DJNZ R7,DL1
    RET


TAB:
DB	3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH	  不带小数点显示
TABEL:
DB	0BFH,86H,0DBH,0CFH,0E6H,0EDH,0FDH,0F7H,0FFH,0EFH    带小数点显示	
END

[修改于 3 年前 - 2016-07-08 23:30:56]

想参与大家的讨论?现在就 登录 或者 注册

{{submitted?"":"投诉"}}
请选择违规类型:
{{reason.description}}
支持的图片格式:jpg, jpeg, png