motu
13-04-2014, 02:09 AM
Chào các bác, e vừa mua 1con module LED7, mà ác cái nó xài MAX7219 để điều khiển, con này lại dùng giao tiếp SPI, e chưa thử bao giờ, vật lộn với nó cả ngày chẳng xong nên nhờ các bác giúp đỡ, code e test như sau:
#include <16F877A.H>
#include <def_877a.h>
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#include <DS1820.c>
#include <Temperature.c>
#define Chip_Select PIN_C0 //RC0 Chip select
// Here we set the configuration of max7219.
void max7219_init1()
{
output_low(Chip_Select); // SELECT MAX
SPI_write(0x09); // BCD mode for digit decoding
SPI_write(0x00);
output_high(Chip_Select); // DESELECT MAX
output_low(Chip_Select); // SELECT MAX
SPI_write(0x0A);
SPI_write(0x0F); // Segment luminosity intensity
output_high(Chip_Select); // DESELECT MAX
output_low(Chip_Select); // SELECT MAX
SPI_write(0x0B);
SPI_write(0x07); // Display refresh
output_high(Chip_Select); // DESELECT MAX
output_low(Chip_Select); // SELECT MAX
SPI_write(0x0C);
SPI_write(0x01); // Turn on the display
output_high(Chip_Select); // DESELECT MAX
output_low(Chip_Select); // SELECT MAX
SPI_write(0x00);
SPI_write(0xFF); // No test
output_high(Chip_Select); // DESELECT MAX
}
void main()
{
SET_TRIS_C(0x00);
setup_spi(spi_master|spi_l_to_h|spi_clk_div_16);
Delay_ms(1000);
max7219_init1(); // initialize max7219.
Delay_ms(1000);
do // infinite loop.
{
output_low(Chip_Select); // select max7219.
SPI_write(0x02); // send digit place.
Delay_us(10);
SPI_write(0x01); // send value to max7219.
output_high(Chip_Select);
}
while(1); // do forever.
}
Nó chẳng nhúc nhích gì cả, em test từng dòng lệnh thì thấy nó treo ngay lệnh SPI_write() đầu tiên, bác nào rành giao tiếp SPI giúp e với, đuối quá rồi. Thk các bác trước.
#include <16F877A.H>
#include <def_877a.h>
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#include <DS1820.c>
#include <Temperature.c>
#define Chip_Select PIN_C0 //RC0 Chip select
// Here we set the configuration of max7219.
void max7219_init1()
{
output_low(Chip_Select); // SELECT MAX
SPI_write(0x09); // BCD mode for digit decoding
SPI_write(0x00);
output_high(Chip_Select); // DESELECT MAX
output_low(Chip_Select); // SELECT MAX
SPI_write(0x0A);
SPI_write(0x0F); // Segment luminosity intensity
output_high(Chip_Select); // DESELECT MAX
output_low(Chip_Select); // SELECT MAX
SPI_write(0x0B);
SPI_write(0x07); // Display refresh
output_high(Chip_Select); // DESELECT MAX
output_low(Chip_Select); // SELECT MAX
SPI_write(0x0C);
SPI_write(0x01); // Turn on the display
output_high(Chip_Select); // DESELECT MAX
output_low(Chip_Select); // SELECT MAX
SPI_write(0x00);
SPI_write(0xFF); // No test
output_high(Chip_Select); // DESELECT MAX
}
void main()
{
SET_TRIS_C(0x00);
setup_spi(spi_master|spi_l_to_h|spi_clk_div_16);
Delay_ms(1000);
max7219_init1(); // initialize max7219.
Delay_ms(1000);
do // infinite loop.
{
output_low(Chip_Select); // select max7219.
SPI_write(0x02); // send digit place.
Delay_us(10);
SPI_write(0x01); // send value to max7219.
output_high(Chip_Select);
}
while(1); // do forever.
}
Nó chẳng nhúc nhích gì cả, em test từng dòng lệnh thì thấy nó treo ngay lệnh SPI_write() đầu tiên, bác nào rành giao tiếp SPI giúp e với, đuối quá rồi. Thk các bác trước.