用RA0控制讓七段顯示器顯示1-9
不知道是我不太懂還是哪裡出了問題就是不能顯示
#include <pic.h>
#include "cnfig877a.h"
//**********************************
//* Function Prototype Declaration
//**********************************
void Init_IO(void);
void Init_Adc( void );
void A2D( unsigned char );
void C( unsigned char );
// ================================================================
// **** Establish PIC16F877A Configuration Word
// **** == HS Oscillator Mode
// **** == Brown-Out Detect Enabled
// **** == Watch-Dog Timer Off
// **** == Code Protect Off
// **** == Low Voltage Programming Off
// **** == ICD2 Debug Mode On
__CONFIG ( XT_OSC & BODEN_OFF & WDT_OFF & CP_OFF & LVP_OFF & DEBUG_ON );
// =================================================================
unsigned int AD_Temp;
union {
int AD_10bit;
struct {
char AD_LSB;
char AD_MSB;
}AD_Byte;
}AD_Int;
/*****************************
INITIALIZE I/O PORT
*****************************/
void Init_IO(void)
{
ADCON1=0b00000110;
}
/*****************************************
Initialize A/D & Comparator Module
******************************************/
void Init_Adc( void )
{
CMCON=0b000000111; // Analog comparator disable
ADCON1=0b00001110; // AN0 is A/D input
ADFM=1; // Right justified
ADCON0=0b10000001; // Selext CH0 , A/D on
}
/***********************************
Convert A/D Channel
************************************/
void A2D( unsigned char channel )
{
unsigned char temp; // Temp storage
temp = channel << 3; // Shift channel value three bits left
ADCON0 &= 0b11000111; // Clear channel select bits in ADCON0
ADCON0 |= temp; // Logically OR shifted channel value into place in ADCON0
ADGO = 1; // Initiate conversion on selected channel
while (ADGO) ; // Poll ADGO bit until it clears, indicating conv. done
AD_Int.AD_Byte.AD_MSB=ADRESH;
AD_Int.AD_Byte.AD_LSB=ADRESL;
}
//***************************************
//* Program Main ( ) *
//***************************************
void main(void)
{
Init_IO();
Init_Adc();
while(1)
{
A2D(0);
C=ADC(AD_Temp);
SHOW_DP(C);
}
}
void SHOW_DP(unsigned int DP_Data)
{
AD_Temp=DP_Data;
switch(DP_Data/9)
{
case 1:
PORTB=0x06;
break;
case 2:
PORTB=0x5b;
break;
case 3:
PORTB=0x4f;
break;
case 4:
PORTB=0x66;
break;
case 5:
PORTB=0x6d;
break;
case 6:
PORTB=0x7d;
break;
case 7:
PORTB=0x07;
break;
case 8:
PORTB=0x7f;
break;
case 9:
PORTB=0x6f;
break;
}
}
應該是在 Program Main出問題
請大大多多指教
Edited by Ryang on 2009年09月23日 11:18:00