找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4652|回复: 3
收起左侧

老外的M8 mini逻辑分析仪制作资料

[复制链接]
ID:367645 发表于 2018-7-9 10:38 | 显示全部楼层 |阅读模式
网上找的一个老外的作品,用AVR M8做的一个简易的逻辑分析仪,适合有一定单片机基础的学习者参考学习。喜欢的可以自己玩玩。

制作出来的实物图如下:
miniLogicAnalyzer_pic2.jpg miniLogicAnalyzer_pic1.jpg miniLogicAnalyzer_pic3.jpg

单片机源程序如下:
  1. /*
  2. ** Four-channel mini logic analyzer.
  3. **
  4. ** Target: ATmega8 at 16MHz crystal
  5. **
  6. ** Input pins PD0-PD3
  7. **
  8. ** Frimware v1.00 was written by Vassilis Serasidis on 01 January 2012.
  9. ** avrsite@yahoo.gr, info@serasidis.gr
  10. **
  11. ** IDE: AVRstudio 4.18 with AVR-gcc plugin (WinAVR-20100110).
  12. **
  13. ** Nokia 3310 / 5110 gLCD library (84*48 pixels) was written by Tony Myatt - 2007
  14. ** Small modifications to the gLCD source code library had been made by Vassilis Serasidis.
  15. **
  16. ** 3310/5110 LCD is connected on AVR's PORT B
  17. ** See <lcd.h> file for pins assignment between AVR and 3310/5110 LCD.
  18. **
  19. **   Nokia LCD    ATmega8
  20. **  ------------  -------
  21. **  LCD_CLK_PIN     PB4
  22. **  LCD_DATA_PIN    PB3
  23. **  LCD_DC_PIN      PB2
  24. **  LCD_RST_PIN     PB1
  25. **  LCD_CE_PIN      PB0
  26. **       
  27. **
  28. */
  29. #include <avr/io.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <util/delay.h>
  34. #include <avr/pgmspace.h>
  35. #include "lcd.h"
  36. #include "introScreen.h"
  37. #include "createdBy.h"

  38. #define BUFFER_SIZE  870  // 290 Sample transitions * 3 bytes for each sample = 870 bytes.
  39. #define POSITIONS_LENGTH 19004860 //BUFFER_SIZE * (MAX_SAMPLE_TIME/3) = (870/3) * 65534 = 19004860
  40. #define ONE_LINE_LENGTH  84
  41. #define MAX_SAMPLE_TIME 65534
  42. #define IN1 0
  43. #define IN2 1
  44. #define IN3 2
  45. #define IN4 3
  46. #define ONE  32
  47. #define ZERO  4
  48. #define TRANSITION 60
  49. #define MIDDLE 36
  50. #define FALSE 1
  51. #define TRUE 0

  52. #define BTN_INCREASE   PD4
  53. #define BTN_DECREASE   PC5
  54. #define BTN_ZOOM_OUT   PD5
  55. #define LED1           PB5

  56. char dataBuffer[BUFFER_SIZE];
  57. volatile unsigned int  i, counter, bufferUsedSpace, zoom, minSampleTime;
  58. volatile unsigned long  samplesPos;
  59. char int2str[8];

  60. void checkIfNoButtonIsPressed (void);
  61. void checkDecreaseButton (void);
  62. void checkIncreaseButton (void);
  63. void printCapturedData (unsigned long position, unsigned char inputChannel);
  64. void printRuler (void);
  65. void saveSampleToBuffer (void);
  66. void checkInputs (void);
  67. void sendChannelsDataOnLCD (void);
  68. void getNextSampleString (void);

  69. //char createdBy1 [] PROGMEM = {"Vassilis"};
  70. //char createdBy2 [] PROGMEM = {"Serasidis"};
  71. //char createdBy3 [] PROGMEM = {"(c)01 Jan 2012"};
  72. //char createdBy4 [] PROGMEM = {"www*serasidis*gr"};

  73. //========================================================================
  74. //  Main program
  75. //========================================================================
  76. int main(void)
  77. {       
  78.        
  79.         DDRD = 0x00;
  80.         PORTD = 0x00;

  81.         DDRC = 0x00;
  82.         PORTC = 0xff;

  83.         DDRB |= (1<<LED1);

  84.         PORTB &= ~(1<<LED1);
  85.         PORTD |= (1<<BTN_INCREASE);     //Enable pull-up resistor on BTN_DECREASE pin.
  86.         PORTC |= (1<<BTN_DECREASE);    //Enable pull-up resistor on BTN_INCREASE pin.
  87.         PORTD |= (1<<BTN_ZOOM_OUT); //Enable pull-up resistor on BTN_ZOOM_OUT pin.

  88.         _delay_ms(100);

  89.         lcd_init();

  90.         _delay_ms(100);

  91.         lcd_contrast(0x40);

  92.         printPictureOnLCD(introScreen);
  93.         printPictureOnLCD(createdBy);
  94.        
  95.         samplesPos = 0;
  96.         bufferUsedSpace = 0;
  97.         zoom = 1;
  98.         minSampleTime = MAX_SAMPLE_TIME;
  99.         counter = 0;

  100.         lcd_clear();
  101.         lcd_goto_xy(3,3);
  102.         lcd_str("Waiting for");
  103.         lcd_goto_xy(4,4);
  104.         lcd_str("signal...");
  105.        
  106.         checkInputs(); // Stay here until a logic level change will be made on PORT D.
  107.                        // Then, read all changes on PORT D until dataBuffer will be full.

  108.         lcd_clear();
  109.         lcd_goto_xy(14,1);
  110.         lcd_chr('1');
  111.         printRuler();
  112.         sendChannelsDataOnLCD();
  113.        
  114.         for(;;)
  115.         {
  116.                 checkDecreaseButton();
  117.                 checkIncreaseButton();
  118.                 checkIfNoButtonIsPressed();
  119.         }
  120. }


  121. //========================================================================
  122. //
  123. //========================================================================
  124. void getNextSampleString (void)
  125. {
  126.         unsigned int w;

  127.         if(bit_is_clear(PINC,BTN_DECREASE))
  128.         {
  129.                 for(w=0; w<BUFFER_SIZE;w++) //Clear the buffer for next sample string.
  130.                         dataBuffer[w] = 0xFF;

  131.                 samplesPos = 0;
  132.                 bufferUsedSpace = 0;
  133.                 zoom = 0;
  134.                 minSampleTime = MAX_SAMPLE_TIME;
  135.                 counter = 0;
  136.                
  137.                 lcd_clear();
  138.                 lcd_goto_xy(3,3);
  139.                 lcd_str("Waiting for");
  140.                 lcd_goto_xy(4,4);
  141.                 lcd_str("signal...");
  142.                 checkInputs();
  143.                 lcd_clear();
  144.                 lcd_goto_xy(14,1);
  145.                 lcd_chr('1');
  146.                 printRuler();
  147.         }
  148. }
  149. //========================================================================
  150. //
  151. //========================================================================
  152. void checkInputs (void)
  153. {

  154.         unsigned char dataPins;

  155.         dataPins = PIND; //Read the pins status on Port D (this is the initial PORT D status).

  156.         while (dataPins == PIND); //Stay here until at least one pin on PORT D changes its Logic status.

  157.         PORTB |= (1<<LED1); //Turn-ON the LED. Start recording sample lengths to the AVR's dataBuffer.
  158.         while(bufferUsedSpace < BUFFER_SIZE) //While the used buffer is not full, repeat this loop.
  159.         {
  160.                 if((dataPins == PIND)&&(counter < MAX_SAMPLE_TIME)) // If the PORT D has the same status as before, just...
  161.                         counter++;                                                                                //... increase the counter.
  162.                 else  // or else,
  163.                 {       
  164.                         saveSampleToBuffer(); //save to the buffer the length of the pulse.
  165.                         dataPins = PIND;      //Read the pins status on Port D.
  166.                 }       
  167.         }
  168.         PORTB &= ~(1<<LED1); //Turn-OFF the LED. That means that AVR has filled its dataBuffer.
  169. }
  170. //========================================================================
  171. //
  172. //========================================================================
  173. void checkIfNoButtonIsPressed (void)
  174. {
  175.         unsigned int w;

  176.         if(bit_is_clear(PIND,BTN_ZOOM_OUT))
  177.         {
  178.                 while(bit_is_clear(PIND,BTN_ZOOM_OUT));
  179.                         getNextSampleString();
  180.                 if((zoom > 0)&&(zoom < 8192))
  181.                         zoom *= 2;
  182.                 else
  183.                         zoom = 1;
  184.                
  185.                 lcd_clear_area(1,61,83);
  186.                 itoa(zoom,int2str,10);
  187.                 w = strlen(int2str);
  188.                 lcd_goto_xy(15 - w,1);
  189.                 lcd_str(int2str);

  190.                 sendChannelsDataOnLCD();
  191.         }
  192. }

  193. //========================================================================
  194. //
  195. //========================================================================
  196. void checkIncreaseButton (void)
  197. {
  198.         if(bit_is_clear(PIND,BTN_INCREASE)&&(samplesPos < POSITIONS_LENGTH)) //Scroll to the left the waveform content.
  199.         {

  200.                 if(counter < MAX_SAMPLE_TIME)
  201.                 {
  202.                         if(samplesPos < (POSITIONS_LENGTH - zoom))
  203.                                 samplesPos += zoom;
  204.                         sendChannelsDataOnLCD();
  205.                 }
  206.         }
  207. }               

  208. //========================================================================
  209. //
  210. //========================================================================
  211. void checkDecreaseButton (void)
  212. {
  213.         if(bit_is_clear(PINC,BTN_DECREASE)&&(samplesPos > 0)) //Scroll to the right the waveform content.
  214.         {
  215.                 if(counter < MAX_SAMPLE_TIME)
  216.                 {
  217.                         if(samplesPos >= zoom)
  218.                                 samplesPos -= zoom;
  219.                         sendChannelsDataOnLCD();
  220.                 }
  221.         }
  222. }

  223. //========================================================================
  224. //
  225. //========================================================================
  226. void printCapturedData (unsigned long position, unsigned char inputChannel)
  227. {
  228.         unsigned int m,j, b,sampleLength,n,samplLength;
  229.         unsigned char w, sample, nextSample = 0;
  230.         unsigned long lengthSum,oldLength;

  231.         lcd_clear_area(1,1,42);

  232.         ltoa(position,int2str,10);
  233.         w = strlen(int2str);
  234.         lcd_goto_xy(9-w,1);
  235.         lcd_str(int2str);
  236.         lcd_goto_xy(1,inputChannel + 3); //Go to LCD line 3, 4, 5 or 6.

  237.         n = 0;
  238.         w = 1;
  239.         b = 0;
  240.         lengthSum = 0;
  241.         oldLength = 0;
  242.         samplLength = 0;

  243.         sampleLength = dataBuffer[b];
  244.         sampleLength <<= 8; //HIGH byte of counter
  245.         sampleLength |= dataBuffer[b+1]; //LOW byte of counter

  246.         do
  247.         {
  248.                 lengthSum += sampleLength;
  249.                 if(samplesPos > lengthSum)
  250.                 {
  251.                         oldLength += sampleLength;
  252.                         b += 3; //Each sample has 3 bytes. 2 for sample length and 1 for the PIN D status data.
  253.                         sampleLength = dataBuffer[b];
  254.                         sampleLength <<= 8; //HIGH byte of counter
  255.                         sampleLength |= dataBuffer[b+1]; //LOW byte of counter
  256.                 }
  257.         }while(samplesPos > lengthSum);

  258.         for(m=b;m<BUFFER_SIZE;m+=3)
  259.         {
  260.                 sampleLength = dataBuffer[m];
  261.                 sampleLength <<= 8; //HIGH byte of counter
  262.                 sampleLength |= dataBuffer[m+1]; //LOW byte of counter

  263.                 if(w > 0)
  264.                 {
  265.                         sampleLength -= (samplesPos - oldLength);
  266.                         w=0;                       
  267.                 }
  268.                
  269.                 sample = dataBuffer[m+2];
  270.                 if((m+5) < BUFFER_SIZE)
  271.                 {
  272.                         nextSample = dataBuffer[m+5];
  273.                         nextSample ^= sample;
  274.                 }
  275.                 samplLength = sampleLength;
  276.                 if(zoom > 0)
  277.                         samplLength /= zoom;

  278.                 for(j=0;j<samplLength;j++)
  279.                 {
  280.                         if(bit_is_set(sample,inputChannel))
  281.                                 lcd_col(ONE); //1
  282.                         else
  283.                                 lcd_col(ZERO);//0
  284.                         n++;

  285.                         if(n >= ONE_LINE_LENGTH)
  286.                                 break;
  287.                 }
  288.                 if(bit_is_set(nextSample,inputChannel))
  289.                 {
  290.                                 lcd_pixelBack();
  291.                                 lcd_col(TRANSITION);
  292.                                 nextSample = 0; //Set nextSample to <No Sample> status.
  293.                 }

  294.                 if(n >= ONE_LINE_LENGTH)
  295.                         break;
  296.         }
  297. }


  298. //========================================================================
  299. //
  300. //========================================================================
  301. void printRuler (void)
  302. {
  303.         unsigned char k;

  304.         lcd_goto_xy(1,2); //Go to LCD line 2.

  305.         for(k=0;k<8;k++)
  306.         {
  307.                 lcd_col(TRANSITION);
  308.                 lcd_col(0b00000000);
  309.                 lcd_col(0b00011000);
  310.                 lcd_col(0b00000000);
  311.                 lcd_col(0b00011000);
  312.                 lcd_col(0b00000000);
  313.                 lcd_col(0b00011000);
  314.                 lcd_col(0b00000000);
  315.                 lcd_col(0b00011000);
  316.                 lcd_col(0b00000000);
  317.         }
  318.         lcd_col(TRANSITION);
  319.         lcd_col(0b00000000);
  320.         lcd_col(0b00011000);
  321.         lcd_col(0b00000000);
  322. }

  323. //========================================================================
  324. //
  325. //========================================================================
  326. void saveSampleToBuffer (void)
  327. {
  328. ……………………

  329. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
0.png

所有资料51hei提供下载:

miniLogicAnalyzer.rar (1.2 MB, 下载次数: 36)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:56204 发表于 2018-10-25 06:07 来自手机 | 显示全部楼层
一直想做个测试仪,这个很好下载
回复

使用道具 举报

ID:51443 发表于 2018-10-25 18:23 | 显示全部楼层
本帖最后由 职教电子 于 2018-11-3 17:22 编辑

原来没看出来用的是啥开发环境
回复

使用道具 举报

ID:51443 发表于 2018-11-7 12:15 | 显示全部楼层
不知道这个如果要改用progisp下载程序,熔丝位该怎样设置?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表