سلام دوستان میشه این برنامه رو چک کنید ببینم مشکل کجاست؟
با pic کار نکردم بلد نیستم لطفا کمکم کنید
مدار رو میبندم کل 7سگمنت روشن میشه!!!
با pic کار نکردم بلد نیستم لطفا کمکم کنید
مدار رو میبندم کل 7سگمنت روشن میشه!!!
کد:
[// Define Soft-SPI connections for LED display #define MOSI_Pin RC0_bit #define CS_Pin RC1_bit #define CLK_Pin RC2_bit // Define Data pin for DHT11 #define Data RC4_bit #define DataDir TRISC4_bit #define FC_Select RC3_bit // Fahrenheit or Celcious select unsigned short TOUT = 0, CheckSum, i, check, u, NumSamples; unsigned short T_Byte1, T_Byte2, RH_Byte1, RH_Byte2; unsigned long int brightness=0x0b; unsigned int counter = 0, TempF, TempC, RH, ADC_OP, TempSum, RHSum; //unsigned short Brightness_Table[21] = {0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 15, 15, 15}; unsigned short Brightness_Table[21] = {0, 1, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10, 11, 12, 12, 13, 14, 15, 15}; void SPI_Write_Byte(unsigned short num){ unsigned short t, Mask, Flag; CLK_Pin = 0; Mask = 128; for (t=0; t<8; t++){ Flag = num & Mask; if(Flag == 0) MOSI_Pin = 0; else MOSI_Pin = 1; CLK_Pin = 1; CLK_Pin = 0; Mask = Mask >> 1; } } void MAX7219_INIT() { // Disable Shutdown mode CS_Pin = 0; // CS pin is pulled LOW SPI_Write_Byte(0x0C); // Select Shutdown register SPI_Write_Byte(0x01); // Set D0 bit to return to normal operation CS_Pin = 1; // CS pin is pulled HIGH // Set BCD decode mode for digits DIG1-DIG3, and DIG5-DIG7 CS_Pin = 0; // CS pin is pulled LOW SPI_Write_Byte(0x09); // Select Decode Mode register SPI_Write_Byte(0b00001100); // Disable BCD mode for digits DIG0, DIG1 CS_Pin = 1; // CS pin is pulled HIGH // Set display brighness CS_Pin = 0; // CS pin is pulled LOW SPI_Write_Byte(0x0A); // Select Intensity register SPI_Write_Byte(brightness); // Set brightness CS_Pin = 1; // CS pin is pulled HIGH // Set display refresh CS_Pin = 0; // CS pin is pulled LOW SPI_Write_Byte(0x0B); // Select Scan-Limit register SPI_Write_Byte(0x03); // Select digits DIG0-DIG3 CS_Pin = 1; // CS pin is pulled HIGH // Enable Display-Test CS_Pin = 0; // CS pin is pulled LOW SPI_Write_Byte(0x0F); // Select Display-Test register SPI_Write_Byte(0x01); // Enable Display-Test CS_Pin = 1; // CS pin is pulled HIGH Delay_ms(1000); // Disable Display-Test CS_Pin = 0; // CS pin is pulled LOW SPI_Write_Byte(0x0F); // Select Display-Test register SPI_Write_Byte(0x00); // Disable Display-Test CS_Pin = 1; // CS pin is pulled HIGH } void Display_Value(unsigned int j, unsigned short k){ CS_Pin = 0; // CS pin is pulled LOW SPI_Write_Byte(4); // Send thousands digit SPI_Write_Byte((j/100)%10); CS_Pin = 1; // CS pin is pulled HIGH CS_Pin = 0; // CS pin is pulled LOW SPI_Write_Byte(3); // Send hundreds digit SPI_Write_Byte(((j/10)%10)); CS_Pin = 1; // CS pin is pulled HIGH CS_Pin = 0; // CS pin is pulled LOW SPI_Write_Byte(2); // Send tens digit if(k <= 1) SPI_Write_Byte(0x63); // Display Degree symbol if(k == 2) SPI_Write_Byte(0x00); // Blank during RH CS_Pin = 1; // CS pin is pulled HIGH CS_Pin = 0; // CS pin is pulled LOW SPI_Write_Byte(1); // Send ones digit if(k == 0) SPI_Write_Byte(0x47); // Display 'F' if(k == 1) SPI_Write_Byte(0x4E); // Display C if(k == 2) SPI_Write_Byte(0x67); // Display P CS_Pin = 1; // CS pin is pulled HIGH } void StartSignal(){ DataDir = 0; // Data port is output Data = 0; Delay_ms(25); Data = 1; Delay_us(30); DataDir = 1; // Data port is input } unsigned short CheckResponse(){ TOUT = 0; TMR1L = 0; TMR1H = 0; T1CON.TMR1ON = 1; // start timer while(!Data && !TOUT); if (TOUT) return 0; else { TMR1L = 0; TMR1H = 0; while(Data && !TOUT); if (TOUT) return 0; else { T1CON.TMR1ON = 0; return 1; } } } unsigned short ReadByte(){ unsigned short num = 0; DataDir = 1; for (i=0; i<8; i++){ while(!Data); TMR1L = 0; TMR1H = 0; T1CON.TMR1ON = 1; while(Data); T1CON.TMR1ON = 0; if(TMR1L > 40) num |= 1<<(7-i); // If time > 40us, Data is 1 } return num; } void interrupt(){ if(PIR1.TMR1IF){ TOUT = 1; T1CON.TMR1ON = 0; // stop timer PIR1.TMR1IF = 0; // Clear TMR0 interrupt flag } } void Display_Brightness(){ ADC_OP = ADC_Read(2); Brightness = ADC_OP/50; CS_Pin = 0; // CS pin is pulled LOW SPI_Write_Byte(0x0A); // Select Intensity register SPI_Write_Byte(Brightness_Table[brightness]); // Update brightness CS_Pin = 1; // CS pin is pulled HIGH } void Wait_Nsec(){ Delay_ms(3000); } void main() { ANSEL = 0b00000100; // RA2/AN2 is analog input CMCON0 = 0x07 ; // Disbale comparators TRISC = 0b00001000; // PORTC All Outputs except RC3 TRISA = 0b00001110; // PORTA All Outputs, Except RA3 and RA2 INTCON.GIE = 1; //Enable global interrupt INTCON.PEIE = 1; //Enable peripheral interrupt // Configure Timer2 module PIE1.TMR1IE = 1; // Enable Timer2 interrupt T1CON = 0; // Prescaler 1:1, and Timer2 is off initially PIR1.TMR1IF =0; // Clear TMR INT Flag bit TMR1H = 0; TMR1L = 0; MAX7219_INIT(); // initialize max7219 if(!FC_Select) Display_Value(0, 0); if(FC_Select) Display_Value(0, 1); do{ NumSamples = 0; TempSum = 0; RHSum = 0; for (u=0; u<4; u++) { StartSignal(); check = CheckResponse(); if (!check) { RH = 0; TempC = 0; } else{ RH_Byte1 = ReadByte(); RH_Byte2 = ReadByte(); T_Byte1 = ReadByte(); T_Byte2 = ReadByte(); CheckSum = ReadByte(); // Check for error in Data reception if (CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)) { TempC = 10*T_Byte1; RH = 10*RH_Byte1; TempSum = TempSum + TempC; RHSum = RHSum + RH; NumSamples ++; } else{ TempC = 0; RH = 0; } } Delay_ms(2000); } // End for loop RH = RHSum/NumSamples; TempC = TempSum/NumSamples; TempF = 9*TempC/5 + 320; Display_Brightness(); Display_Value(RH, 2); Wait_Nsec(); Display_Brightness(); if(!FC_Select) Display_Value(TempF, 0); if(FC_Select) Display_Value(TempC, 1); }while(1); }
دیدگاه